c# - (MVC4 - Razor) List of model in a model don't caught by the post method -


i have mvc application model :

public class listofmymodel {     public list<mymodel> mymodels { get; set; }     public guid myid { get; set; } }  public class mymodel {     // code :     public string mystring { get; set; } } 

and post method in controller :

[httppost] public actionresult editme(listofmodel mylist) {     try     {         if (modelstate.isvalid)         {             list<mymodel> mymodels = mylist.mymodels;              foreach (mymodel model in mymodels)                 // code              return redirecttoaction("index");         }     catch     {         // code         return view(mylist)      }     return view(mylist); } 

and view : ( use kendo ui ) ( p.s : code has been stripped away , replaced comment code )

@model myapplication.web.models.listofmymodel @{ viewbag.title = mytitle; layout = "~/views/shared/_mylayout.cshtml";  }  <div class="span1"></div> <div class="span8">     <div id="list-wrapper">         <div class="k-content">             <form id="form" class="form-horizontal span8 offset2" method="post" action="@url.action("editme")">              <script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script>             <script src="@url.content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>             <script src="@url.content("~/scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>             <script src="@url.content("~/scripts/kendo/2013.1.514/kendo.web.min.js")"></script>             <script src="@url.content("~/scripts/kendo/2013.1.514/kendo.aspnetmvc.min.js")"></script>               <div class="offset2 span2">                 <fieldset>                     <legend> title </legend>                     <p>some code :</p>                     @html.hiddenfor(m => m.myid)                     @for (int = 0; < model.mymodels.count; i++)                     {                     // code                     <div class="control-group">                         <label class="control-label">mytext : </label>                         <div class="controls">                         @(html.kendo().dropdownlistfor(c =>                 model.mymodels[i].mystring)                                         .datatextfield("text")                                         .datavaluefield("value")                                         .datasource(datasource => datasource                                              .read(read => read.action("getsomethings", "mycontroller"))                                         )                                         .value(model.mymodels[i].mystring)                                     )                          </div>                       </div>                      }                      <div class="form-actions">                          <button type="submit" class="btn btn-primary">validate</button>                     </div>                   </fieldset>               </div>            </form>         </div>     </div>  </div> 

but problem when push submit button in view, method of controller called data expected ( saw in chrome ) in method, of model null : id , list... don't known problem ?

thank reading , trying understand this, if want more informations please tell me.

the myid should correctly received existing code.

the model binder can match value of inputs name have matching property in form. i.e. if have input <input name='bikes' ...> model should have property bikes model binder can transfer value input property. in case you're creating input dynamic names doesn't have matching property name. (note: thei referes model you're using action parameter).

the farthest can go giving series of input elements same name, i.e. several elements <input name='categories' ...> , receive in array parameter string[] categories or model has property string[] categories {get;set;}

in short, have redesign model , view. if used list<string> instead of list<mymodel>, , view had fixed name dropdow lists, dropdownlistfor(c => model.mymodels, model binder fill mymodels property list of selected strings in each drop down list.

hint: can use model view , receive different model (or series of parameters) in action. in way can send more information render view, , receive post essential data process user input.

see answer question alternatives. explains similar question.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -