简体   繁体   中英

IEnumerable data not passing back from view to controller

In my MVC4 Project view am passing IEnumerable<Settings> model to view. When i submit the form back to my controller, the controller method is having parameter as IEnumerable<Settings> data . The problem is that data is always null.

View

@model IEnumerable<Settings>

@using (Html.BeginForm("Index", "Layout", FormMethod.Post)))
{
    @foreach (var item in Model)
    {
       @Html.CheckBoxFor(modelItem => item.IsActive, new { @value = item.Id })
       @Html.DisplayFor(modelItem => item.Name)
    }
    <input type="submit" value="Save" />
}

Controller

[HttpPost]
public ActionResult Index(IEnumerable<Settings> data)
{
    --actual code goes here
}

Model

[Serializable]
public class Settings
{   
    public virtual byte Id { get; set; }

    public virtual string Name { get; set; }

    public virtual bool IsActive { get; set; }

    public virtual DataTable DataResult { get; set; }

    public virtual int CreatedBy { get; set; }

    public virtual DateTime CreatedDate { get; set; }

    public virtual int ClientId { get; set; }

    public virtual Int16 NoofItems { get; set; }
}

Update: * Rendered HTML *

<td>
    <input checked="checked" data-val="true" data-val-required="The IsActive field is required." id="item_IsActive" name="item.IsActive" type="checkbox" value="13" class="valid"><input name="item.IsActive" type="hidden" value="false">
                                # of pieces

                                </td>

Update1: Rendered HTML after following answer by Andrei

<td>
     <input checked="checked" data-val="true" data-val-required="The IsActive field is required." name="[0].IsActive" type="checkbox" value="13"><input name="[0].IsActive" type="hidden" value="false">
                                    # of pieces</td>
@Html.CheckBoxFor(modelItem => item.IsActive, new { @value = item.Id })

is not going to generate right name for the input. Your form sends the same token item.IsActive=true (or false) for all items. While this approach works for primitives, it does not work for complex types.

However here is what you can do using for instead of foreach :

@model IList<Settings>

@using (Html.BeginForm())
{
    @for(int i=0; i<Model.Count; i++)
    {
       @Html.CheckBoxFor(m => m[i].IsActive, new { @value = Model[i].Id })
       @Html.DisplayFor(m => m[i].Name)
    }
    <input type="submit" value="Save" />
}

Notice that this requires at least IList<> as a view type - otherwise you cannot iterate through the collection using for .

Might need to wrap your inputs in a form:

@using (Html.BeginForm()) {
   @foreach (var item in Model)
   {
      @Html.CheckBoxFor(modelItem => item.IsActive, new { @value = item.Id })
      @Html.DisplayFor(modelItem => item.Name)
   }
   <input type="submit" value="Save" />
}

Can you post the renderred html?

Here is the problem I think: The names of the inputs (checkboxes) are going to be IsActive in html. When you hit submit, the form will submit the data with key IsActive . In your controller you are currently expecting a parameter called data so there is a mismatch.

You can troubleshoot this by adding a breakpoint in the controller and checking whether Request["IsActive"] has a value.

UPDATE: You can take a look here for a walkthrough:

I think you can try first to change the name of the input to data . You can try to do that with:

@Html.CheckBoxFor(modelItem => item.IsActive, new { @value = item.Id, name="data" })

After you change it, the form should submit a comma separated list of values to the controller. Can you open firebug as well and post here what gets posted to the controller?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM