asp.net - passing viewmodel correctly - changes not showing on POST -


i having trouble app. when user dynamically adds rows (javascript) add more 'claimlines' 'claim' posted values don't appear, 'dummy' values set viewmodel in first place. user amending or indeed adding (new rows) not being recorded on post.

as far can see passing viewmodel between controller , views missing something. have been struggling while, hugely appreciated. im new asp.net mvc , indeed programming in general. im following steven sanderson's blog.

viewmodel

using system; using system.collections.generic; using system.linq; using system.web; using ef_tut.models; using ef_tut.viewmodels; namespace ef_tut.viewmodels {     public class claimviewmodel     {         public int claimid { get; set; }         public int submissionuserid { get; set; }         public datetime? datesubmitted { get; set; }         public bool approvedyn { get; set; }         public datetime? dateapproved { get; set; }          public icollection<claimline> claimlines { get; set; }      } } 

controller

  using system;     using system.collections.generic;     using system.data;     using system.data.entity;     using system.linq;     using system.web;     using system.web.mvc;     using ef_tut.models;     using ef_tut.dal;     using ef_tut.viewmodels;     using ef_tut.webui.helpers;     namespace ef_tut.controllers     {         public class claimscontroller : controller         {             private claimcontext db = new claimcontext();              private static claimviewmodel _currentclaim;             private static claimviewmodel currentclaim             {                                 {                     _currentclaim = getclaimviewmodel();                     return _currentclaim;                 }                 set                 {                     _currentclaim = value;                 }             }              public static claimviewmodel getclaimviewmodel()             {                 return new claimviewmodel()             {                 claimid =101,                 claimlines = new list<claimline>() {                         new claimline() {claimantuserid =1},                         new claimline() {claimantuserid =2},                         new claimline() {claimantuserid =3}                     }             };}              public actionresult create()             {                 return view(currentclaim);             }              public partialviewresult blankeditorrow()             {                   return partialview("newrow");             }              [httppost]             public actionresult create(claimviewmodel claimviewmodel)             {                 currentclaim = claimviewmodel;                  return view("view2", currentclaim);             }         }     } 

create view

@model ef_tut.viewmodels.claimviewmodel @using ef_tut.models @using ef_tut.viewmodels @{     viewbag.title = "create"; }  <h2>create</h2>  @using (html.beginform()) {     @html.validationsummary(true)      <fieldset>         <legend>claim</legend>                       @html.hiddenfor(model => model.claimid)          <ul id="editorrows">                               @foreach (claimline claimline in model.claimlines)                             {                                 html.renderpartial("newrow", claimline);                 }          </ul>            <p>                @html.actionlink("add another...", "blankeditorrow", null, new { id = "additem" })           </p>     </fieldset>              <input type="submit" value="create" />    @section scripts {     @scripts.render("~/bundles/jqueryval") } }    

partial

@model ef_tut.models.claimline  @using ef_tut.webui.helpers     @using (html.begincollectionitem("claims"))     {         <table class="editorrow">    <tr >                                                       <td>             claimid(claimlinetable)@html.editorfor(model => model.claimid)             </td>         <td>           claimantuserid:  @html.editorfor(model => model.claimantuserid)             </td>                                <td>            hours: @html.editorfor(model => model.hours)             </td>                                                           <td>            <a href="#" class="deleterow">delete</a>             </td>                </tr></table>     } 

output view

@model ef_tut.viewmodels.claimviewmodel @using ef_tut.models  @{     viewbag.title = "view2"; }  <h2>view1</h2>  @html.hiddenfor(model=>model.claimid)     <ul>         @foreach (claimline claimline in model.claimlines){              @claimline.claimantuserid; }     </ul> 

you need use begincollectionitem helper parameter collectionname equal collection name in view model (claimlines). it's because helper replace htmlfieldprefix , names generated html fields are: specified_collection_name[generated_index].claimline_property_name

so in case generated names inputs like: claims[generated_index].hours

but mvc model binder can't find property claims in claimviewmodel.

if replace @using (html.begincollectionitem("claims")) @using (html.begincollectionitem("claimlines")) generated input names like:

claimlines[generated_index].hours

and model binder generate claimviewmodel expected


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -