asp.net - MVC model binding to nested collection -
i have class:
public class clientinformation{ public string username {get; set;} public icollection<regiondistrictcity> regiondistrictcity { get; set; } public class regiondistrictcity { public string region { get; set; } public string district { get; set; } public string city { get; set; } } }
how should formated name attribute of input elements properties region, distirct, city in html in order make model binder populate collection "icollection regiondistrictcity"?
i tried have action method parameter of type "clientinformation" , html name attributes formated "[index].propertyname" in case property "username" binded.
i tried have action method parameter name "client" , have html names attributes formated "client[index].propertyname" doesn't work. (in tha case if there "list client" populated)
thanks.
in mvc4 should use for loop instead of foreach bind collection. model binder able populate model when submit data.
@for (int = 0; < model.regiondistrictcity.count; i++) { @html.editorfor(model => model.regiondistrictcity[i].region) }
but work if not deleting or adding items collection dynamically. if want that, should use begincollectionitem helper created steve sanderson. http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
Comments
Post a Comment