c# - MVC 4 @Html.DropDownListFor() not passing model values to dropdown -


i new mvc , trying test application feet wet. part of application generate form drop down box. use

@html.dropdownlistfor() generate this, , on create form drop down works fine. when go edit form model value not passing drop down.

selectlist item

 public static string[] onofflist()     {         var ret = new string[] { "on", "off" };         return ret;     } 

form code

@html.dropdownlistfor(model => model.servicecondition, new selectlist(onoffdropdownhelper.onofflist())) 

for instance assume model.servicecondition = "off".

for reason whenever call form dropdown value "on", seems ignoring model value.

i have tried this

@html.dropdownlistfor(model => model.servicecondition, new selectlist(onoffdropdownhelper.onofflist(), "off")) 

to mandate "off" value, still coming "on" selected value in drop down.

i reiterate, know model value "off", , created identical "create" form using same @html.dropdownlistfor() , able pass value model fine.

like said, new mvc appreciated.

thanks.

i think have set isselected property. works me:

first, put property in model tidy view code:

public list<selectlistitem> onoffddl {         {         return onoffdropdownhelper.onofflist()             .select(s => new selectlistitem                          {                              text = s,                              value = s,                              selected = servicecondition == s                          })             .tolist();     } } 

then do:

@html.dropdownlistfor(model => model.servicecondition, model.onoffddl) 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -