将列表从视图传递到控制器 MVC3
Passing list from view to controller MVC3
经过深入搜索,我找不到任何解决方案。
我有一个 Ajax.BeginForm 需要传递一个包含有关已检查行的信息的列表。
UnitsChecked Class(型号)
public class UnitsChecked
{
public bool IsChecked { get; set; }
public TaxiTV.Data.Entity.Unit Unit { get; set; }
}
ReportsVM(查看模型 Class)
public class ReportsVM
{
public IEnumerable<UnitsChecked> UnitList { get; set; }
public ReportsVM()
{
UnitList = new List<UnitsChecked>();
}
}
Index.cshtml(查看)
@using (Ajax.BeginForm("GetReports", "Reports", new AjaxOptions { UpdateTargetId = "ReportList", LoadingElementId = "loader" }))
{
<table class="tableBorder">
<tr>
<th>Choose
</th>
<th>
@Resources.IMEI
</th>
<th>
@Resources.SIM
</th>
<th>
@Resources.Name
</th>
</tr>
@for (int i = 0; i < Model.UnitList.Count(); i++ )
{
<tr>
<td>
@Html.CheckBoxFor(m => m.UnitList.ElementAt(i).IsChecked, new { @class = "checkboxListener" })
</td>
<td>
@Html.DisplayFor(m=> m.UnitList.ElementAt(i).Unit.IMEI)
@Html.Hidden("zzz", Model.UnitList.ElementAt(i)) <---- This line
</td>
<td>
@Model.UnitList.ElementAt(i).Unit.SIM
</td>
<td>
@Model.UnitList.ElementAt(i).Unit.Person.FirstName @Model.UnitList.ElementAt(i).Unit.Person.LastName
</td>
</tr>
}
</table>
}
主要的和最重要的线是箭头指向的地方。
使用 Fiddler2,我设法看到所有 UnitsChecked 列表都被一一发送。
以下控制器表示 "zzz" 为空。
[HttpPost]
public ActionResult GetReports(ReportsVM rvm, List<UnitsChecked> zzz)
{
有什么建议吗?
谢谢。
你需要把属性UnitList
改成typeofIList<UnitsChecked>
然后在视图
@for (int i = 0; i < Model.UnitList.Count; i++ )
{
@Html.CheckBoxFor(m => m.UnitList[i].IsChecked, new { @class = "checkboxListener" })
@Html.DisplayFor(m=> m.UnitList.[i].Unit.IMEI)
@Html.HiddenFor(m=> m.UnitList.[i].Unit.SomeProperty) // for any properties of Unit that you want posted back
}
这将正确命名您的控件和索引器,以便它们可以绑定到一个集合,即
<input type="checkbox" name="UnitList[0].IsChecked" .. value="True" />
<input type="hidden" name="UnitList[0].IsChecked" .. value="False" />
<input type="checkbox" name="UnitList[1].IsChecked" .. value="True" />
<input type="hidden" name="UnitList[1].IsChecked" .. value="False" />
您当前对 .ElementAt(i)
的使用为控件提供了重复的 id
属性(无效 html)和 name
属性(因此无法绑定到集合)。
然后您的控制器需要
[HttpPost]
public ActionResult GetReports(ReportsVM rvm)
经过深入搜索,我找不到任何解决方案。 我有一个 Ajax.BeginForm 需要传递一个包含有关已检查行的信息的列表。
UnitsChecked Class(型号)
public class UnitsChecked
{
public bool IsChecked { get; set; }
public TaxiTV.Data.Entity.Unit Unit { get; set; }
}
ReportsVM(查看模型 Class)
public class ReportsVM
{
public IEnumerable<UnitsChecked> UnitList { get; set; }
public ReportsVM()
{
UnitList = new List<UnitsChecked>();
}
}
Index.cshtml(查看)
@using (Ajax.BeginForm("GetReports", "Reports", new AjaxOptions { UpdateTargetId = "ReportList", LoadingElementId = "loader" }))
{
<table class="tableBorder">
<tr>
<th>Choose
</th>
<th>
@Resources.IMEI
</th>
<th>
@Resources.SIM
</th>
<th>
@Resources.Name
</th>
</tr>
@for (int i = 0; i < Model.UnitList.Count(); i++ )
{
<tr>
<td>
@Html.CheckBoxFor(m => m.UnitList.ElementAt(i).IsChecked, new { @class = "checkboxListener" })
</td>
<td>
@Html.DisplayFor(m=> m.UnitList.ElementAt(i).Unit.IMEI)
@Html.Hidden("zzz", Model.UnitList.ElementAt(i)) <---- This line
</td>
<td>
@Model.UnitList.ElementAt(i).Unit.SIM
</td>
<td>
@Model.UnitList.ElementAt(i).Unit.Person.FirstName @Model.UnitList.ElementAt(i).Unit.Person.LastName
</td>
</tr>
}
</table>
}
主要的和最重要的线是箭头指向的地方。 使用 Fiddler2,我设法看到所有 UnitsChecked 列表都被一一发送。 以下控制器表示 "zzz" 为空。
[HttpPost]
public ActionResult GetReports(ReportsVM rvm, List<UnitsChecked> zzz)
{
有什么建议吗? 谢谢。
你需要把属性UnitList
改成typeofIList<UnitsChecked>
然后在视图
@for (int i = 0; i < Model.UnitList.Count; i++ )
{
@Html.CheckBoxFor(m => m.UnitList[i].IsChecked, new { @class = "checkboxListener" })
@Html.DisplayFor(m=> m.UnitList.[i].Unit.IMEI)
@Html.HiddenFor(m=> m.UnitList.[i].Unit.SomeProperty) // for any properties of Unit that you want posted back
}
这将正确命名您的控件和索引器,以便它们可以绑定到一个集合,即
<input type="checkbox" name="UnitList[0].IsChecked" .. value="True" />
<input type="hidden" name="UnitList[0].IsChecked" .. value="False" />
<input type="checkbox" name="UnitList[1].IsChecked" .. value="True" />
<input type="hidden" name="UnitList[1].IsChecked" .. value="False" />
您当前对 .ElementAt(i)
的使用为控件提供了重复的 id
属性(无效 html)和 name
属性(因此无法绑定到集合)。
然后您的控制器需要
[HttpPost]
public ActionResult GetReports(ReportsVM rvm)