c# - Form sends empty model -
c# - Form sends empty model -
i have list of objects want modify on view, have built view model contain list of objects:
public class trainerlistviewmodel { public list<trainer> trainers { get; set; } }
and send list of trainers view controller:
public virtual actionresult social() { var trainers = new trainerlistviewmodel(); trainers.trainers = (from t in _db.trainers select t).tolist(); homecoming view(trainers); }
and here view:
@model xstreamingfitness.viewmodels.trainerlistviewmodel @using (html.beginform("social", "participant", formmethod.post)) { (int = 0; < model.trainers.count; i++) { <div class="formsep"> <div class="editor-label"> @html.labelfor(m => m.trainers[i].permissionpostfacebook) </div> <div class="editor-field"> @html.editorfor(m => m.trainers[i].permissionpostfacebook) @html.validationmessagefor(m => m.trainers[i].permissionpostfacebook) </div> </div> } <input type="submit" value="save settings" name="submit" /> }
now here post controller method:
[httppost] public virtual actionresult social(trainerlistviewmodel trainers) { homecoming redirecttoaction("profile"); }
but everytime submit, trainers model empty , not sure why happening.
this question has been asked before here @ suggest check mvc3 submit model empty, same principals problem having.
see snippet post.
i see people writing next lambda look modelitem => item.someproperty in views , asking why model binder doesn't correctly bind collection properties on view models.
this won't generate proper name checkbox default model binder able recreate settings collection. recommend reading next blog post improve understand right format model binder expects.
-by darin dimitrov
it way building form goes suggest utilize property editor templates trainer object.
this should work.
hope helps
c# asp.net-mvc-4
Comments
Post a Comment