c# - Model Binding to a List MVC 4 -
is there pattern bind ilist of items view. seem having issues httppost. know phil haack wrote nice article dated , said might have fix mvc 4.
this how if need form displayed each item, , inputs various properties. depends on i'm trying though.
viewmodel looks this:
public class myviewmodel { public list<person> persons{get;set;} }
view(with beginform of course):
@model myviewmodel @for( int = 0; < model.persons.count(); ++i) { @html.hiddenfor(m => m.persons[i].personid) @html.editorfor(m => m.persons[i].firstname) @html.editorfor(m => m.persons[i].lastname) }
action:
[httppost]public viewresult(myviewmodel vm) { ...
note on post properties had inputs available have values. i.e., if person had .ssn property, not available in post action because wasn't field in form.
note way mvc's model binding works, consecutive id's. doing conditionally hide item cause not bind data after 5th item, because once encounters gap in ids, stop binding. if there 10 people, first 4 on postback:
@for( int = 0; < model.persons.count(); ++i) { if(i != 4)//conditionally hide 5th item, { //but bug occurs on postback, items after 5th not bound the list @html.hiddenfor(m => m.persons[i].personid) @html.editorfor(m => m.persons[i].firstname) @html.editorfor(m => m.persons[i].lastname) } }
Comments
Post a Comment