Thursday 15 May 2014

c# - IEnumerable data not passing back from view to controller -


In my MVC4 project view IEnumerable & lt; Settings & gt; When I submit a form back to my controller to see the model, the controller method should be IEnumerable . The problem is that data is always empty.

See

  @Emelumerable & lt; Settings & gt; @Usage (Html.BeginForm ("index", "layout", FormMethod.Post)) {@foreach (preference items in model) {@ Html.CheckBoxFor (modelItem => items.isactive, new {@value = item ) Id}} @html DisplayFor (ModelItem => Item.Net)} & lt; Input type = "submit" value = "save" /> } [  

Controller

  [HTTP Post] Public Action Rational Index (INIM Mereble & Lt; Settings & gt; Data) { - The actual code goes here} {Class}  

Model [serializable] Public class settings {public virtual byte id {get; Set; } Public Virtual String {get} Set; } Public Virtual Bull Iactiv {Received; Set; } Public Virtual Datatable Data Result {get; Set; } Created in Public Virtual India {Received; Set; } Date of the public virtual date created {received; Set; } Received Public Virtual Ent Client ID; Set; } Public Virtual Int16 NoofItems {get; Set; }}

Update: * Rated HTML *

  & lt; Td> & Lt; Input check = "checked" data-val = "true" data-val-required = "ective field is required." Id = "item_IsActive" name = "item.IsActive" type = "checkbox" value = "13" square = "valid" & gt; & Lt; Input name = "item.isactive" type = "hidden" value = "wrong" & gt; # Of pieces & lt; / Td>   

Update1: Rated HTML after the following answers by Andrea

  & lt; Td> & Lt; Input check = "checked" data-val = "true" data-val-required = "ective field is required." Name = "[0] .IsActive" type = "checkbox" value = "13" & gt; & Lt; Input name = "[0] .IsActive" type = "hidden" value = "false" & gt; # Of pieces & lt; / Td>  
  @ html.CheckBoxFor (modelItem => items.isactive, New {@value = item.Id})   

Not generating the correct name for the input Your form is the same token for all items item.IsActive = true (Or false). Although this approach works for priorities, but it does not work for complex types.

However here you can use for instead of foreach : <: / P>

  @model IList & lt; Settings & gt; @using (Html.BeginForm ()) {@for (int i = 0; i  M [I] .name)} & lt; Input type = "submit" value = "save" /> }   

Note that at least IList & lt; & Gt; is required as a visual type - otherwise you can not iterate through the archive using for . .

No comments:

Post a Comment