c# - Invalid Arguments in Razor DropDownList -



c# - Invalid Arguments in Razor DropDownList -

i'm receiving error dropdownlist has invalid arguments. what's odd have structured dropdownlist working fine, no errors. drop downwards error sec 1 (that displays year):

view:

@{ string month = "tempemployments[" + idx + "].employmentstartmonth"; string year = "tempemployments[" + idx + "].employmentstartyear"; } @html.dropdownlist(month, (ienumerable<selectlistitem>)this.viewbag.monthlist,employmentstartmonth, new { @class = "field panel-field employmentdate", @id = month }) @html.validationmessagefor(model => model.employmentstart) @html.dropdownlist(year, (ienumerable<selectlistitem>)this.viewbag.yearlist, employmentstartyear, new { @class = "field panel-field employmentdate", @id = year })

controller:

[httppost] public actionresult addemploymenthistory(int applicantid, int recordnum) { this.viewbag.recordnum = recordnum; this.viewbag.statelist = this.getstatelist(); this.viewbag.monthlist = this.getmonthlist(); this.viewbag.yearlist = this.getyearlist(); homecoming partialview("_employmenthistorypartial"); } private ienumerable<selectlistitem> getyearlist() { list<selectlistitem> yearlist = new list<selectlistitem>(); int current_yr = convert.toint32(datetime.now.year.tostring()); int select_yr = 0; for(int = (current_yr-3); <= current_yr; i++) { select_yr = current_yr - (i - (current_yr-3)); yearlist.add(new selectlistitem() { value = select_yr.tostring(), text = select_yr.tostring() }); } homecoming yearlist; } private ienumerable<selectlistitem> getmonthlist() { list<selectlistitem> monthlist = new list<selectlistitem>(); monthlist.add(new selectlistitem() { value = "jan", text = "jan" }); monthlist.add(new selectlistitem() { value = "feb", text = "feb" }); monthlist.add(new selectlistitem() { value = "mar", text = "mar" }); monthlist.add(new selectlistitem() { value = "apr", text = "apr" }); monthlist.add(new selectlistitem() { value = "may", text = "may" }); monthlist.add(new selectlistitem() { value = "jun", text = "jun" }); monthlist.add(new selectlistitem() { value = "jul", text = "jul" }); monthlist.add(new selectlistitem() { value = "aug", text = "aug" }); monthlist.add(new selectlistitem() { value = "sep", text = "sep" }); monthlist.add(new selectlistitem() { value = "oct", text = "oct" }); monthlist.add(new selectlistitem() { value = "nov", text = "nov" }); monthlist.add(new selectlistitem() { value = "dec", text = "dec" }); homecoming monthlist; }

so created sample project info provided , came :

controller

namespace mvcapplication1.controllers { public class homecontroller : controller { public actionresult index() { this.viewbag.recordnum = 1; this.viewbag.monthlist = this.getmonthlist(); this.viewbag.yearlist = this.getyearlist(); homecoming partialview(); } private ienumerable<selectlistitem> getyearlist() { list<selectlistitem> yearlist = new list<selectlistitem>(); int current_yr = convert.toint32(datetime.now.year.tostring()); int select_yr = 0; (int = (current_yr - 3); <= current_yr; i++) { select_yr = current_yr - (i - (current_yr - 3)); yearlist.add(new selectlistitem() { value = select_yr.tostring(), text = select_yr.tostring() }); } homecoming yearlist; } private ienumerable<selectlistitem> getmonthlist() { list<selectlistitem> monthlist = new list<selectlistitem>(); monthlist.add(new selectlistitem() { value = "jan", text = "jan" }); monthlist.add(new selectlistitem() { value = "feb", text = "feb" }); monthlist.add(new selectlistitem() { value = "mar", text = "mar" }); monthlist.add(new selectlistitem() { value = "apr", text = "apr" }); monthlist.add(new selectlistitem() { value = "may", text = "may" }); monthlist.add(new selectlistitem() { value = "jun", text = "jun" }); monthlist.add(new selectlistitem() { value = "jul", text = "jul" }); monthlist.add(new selectlistitem() { value = "aug", text = "aug" }); monthlist.add(new selectlistitem() { value = "sep", text = "sep" }); monthlist.add(new selectlistitem() { value = "oct", text = "oct" }); monthlist.add(new selectlistitem() { value = "nov", text = "nov" }); monthlist.add(new selectlistitem() { value = "dec", text = "dec" }); homecoming monthlist; } } }

view

@{ //string month = "tempemployments[" + idx + "].employmentstartmonth"; //string year = "tempemployments[" + idx + "].employmentstartyear"; } @html.dropdownlist("some name", (ienumerable<selectlistitem>)this.viewbag.monthlist,"some label", new { @class = "field panel-field employmentdate", @id = "id" }) <!-- @@html.validationmessagefor(model => model.employmentstart) no thought model is--> @html.dropdownlist("some name2", (ienumerable<selectlistitem>)this.viewbag.yearlist, "some label", new { @class = "field panel-field employmentdate", @id = "id2" })

and both dropdowns in test worked 100% fine. makes me believe 1 either id, name, or label not beingness populated correctly. code provided not show how values created

populate sec dropdown mine (with dummy id, label, , name, , see if renders. process of elimination add together them in.

also, general tip, abstracting classes out of controller , own classes. helps maintain things lot cleaner. pass in viewmodels of properties needed. avoids "magic strings" , allows code easier update , debug.

as illustration instead of :

public actionresult index() { this.viewbag.recordnum = 1; this.viewbag.monthlist = this.getmonthlist(); this.viewbag.yearlist = this.getyearlist(); homecoming partialview(); }

i do

public actionresult index() { var viewmodel = new indexviewmodel{ recordnum = 1, monthlist = _someservice.getmonthlist(), yearlist = _someservice.getyearlist() } homecoming partialview(viewmodel); }

then in view don't have worry magic strings. can do

@model.monthlist

c# asp.net-mvc asp.net-mvc-4 razor

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -