c# - Edit and Update in Dropdownlist mvc4 -
i trying implement edit , update in dropdownlist in application. list of values dropdownlist displayed model. selected value not displayed on dropdownlist. selected value populated list of values in dropdownlist.
my model :
public string state public selectlist regionlist { get; set; } public class region { public string id { get; set; } public string name { get; set; } }
view
@foreach (var item in model.addresslist) { @html.dropdownlistfor(model => item.state, new selectlist(model.address.regionlist, "value", "text", model.address.regionlist.selectedvalue)) }
note :
item.state populated value not displayed
model.address.regionlist populated , displayed
controller
public actionresult editaddress(addresstuple tmodel,int addressid) { int country; string customerid = 1; list<addressmodel> amodel = new list<addressmodel>(); amodel = getaddressinfo(customerid, addressid); // returns selected value dropdown foreach (var item in amodel) { country = item.countryid; } list<region> objregion = new list<region>(); objregion = getregionlist(id); // returns list of values dropdown selectlist objlistofregiontobind = new selectlist(objregion, "id", "name", 0); atmodel.regionlist = objlistofregiontobind; tmodel.address = atmodel; tmodel.addresslist = amodel; return view(tmodel); }
for edit in dropdownlist, list of values displayed. selected value not displayed. mistake in code .any suggestions.
edit :
@html.dropdownlistfor(model => model.state, new selectlist(model.regionlist, "id", "name",model.state))
this
model => item.state
is not going work, because hides html helper fact value drop down taken model. correct way implement replacing foreach
for
:
@for (int i=0; i<model.addresslist.count; i++) { @html.dropdownlistfor(model => model.addresslist[i].state, new selectlist(model.address.regionlist, "value", "text", model.address.regionlist.selectedvalue)) }
Comments
Post a Comment