[英]IEnumerable data not passing back from view to controller
In my MVC4 Project view am passing IEnumerable<Settings>
model to view. 在我的MVC4项目视图中,正在传递IEnumerable<Settings>
模型进行查看。 When i submit the form back to my controller, the controller method is having parameter as IEnumerable<Settings> data
. 当我将表单提交回控制器时,控制器方法的参数为IEnumerable<Settings> data
。 The problem is that data
is always null. 问题在于data
始终为空。
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 * 更新: * 呈现的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 Update1: Andrei回答后呈现HTML
<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. 您的表单发送相同的令牌项目item.IsActive=true
(或false)的所有项目。 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
: 但是,这是您可以使用for
代替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
. 请注意,这至少需要IList<>
作为视图类型-否则您无法使用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? 您可以发布renderred html吗?
Here is the problem I think: The names of the inputs (checkboxes) are going to be IsActive
in html. 这是我认为的问题:输入(复选框)的名称在html中将为IsActive
。 When you hit submit, the form will submit the data with key IsActive
. 当您单击提交时,表单将使用键IsActive
提交数据。 In your controller you are currently expecting a parameter called data
so there is a mismatch. 在您的控制器中,当前需要一个名为data
的参数,因此不匹配。
You can troubleshoot this by adding a breakpoint in the controller and checking whether Request["IsActive"] has a value. 您可以通过在控制器中添加断点并检查Request [“ IsActive”]是否具有值来解决此问题。
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
. 我认为您可以首先尝试将输入name
更改为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? 您是否也可以打开Firebug并在此处发布将发布到控制器的内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.