c# - How can I bind an additional property to a DropdownListFor? -
in following code, id of selected country correctly passed controller when form submitted. however, i'd pass along name of country. how can send name along id controller?
model
public class mymodel { public country country { get; set; } public list<country> countrylist { get; set; } } public class country { public int? id { get; set; } public string name { get; set; } }
view
@using (html.beginform()) { <div> @html.dropdownlistfor(m => m.country.id, new selectlist(model.countrylist, "id", "name")) </div> }
controller
mymodel model = new mymodel(); public actionresult index() { using (var service = new service()) { model.countrylist = service.getcountries(); return view(model); } } public actionresult index(mymodel tmpmodel) { return redirecttoaction("index"); }
Comments
Post a Comment