![](/img/trans.png)
[英]Pass values from Hidden field to ActionLink then to Controller
[英]Pass lists of selected relevant hidden field values form to the controller
谁能告诉我如何将选定的相关隐藏字段值的列表从中传递给控制器。 分配的字段返回值为Boolean(true)。 有超过100个列表项。 如果管理员选择(分配)列表并提交,它将把所有列表值传递给控制器并更新数据库。
这是我的视图页面的chtml代码:
@using (Html.BeginForm("CAssigned", "Orders", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@foreach (var item in Model)
{
@Html.HiddenFor(model => item.LocationId)
<td>
@Html.DisplayFor(modelItem => item.MapIcons)
@Html.HiddenFor(model => item.MapIcons,"MapIcons")
</td>
<td>
@Html.EditorFor(modelItem => item.Assigned)
@Html.HiddenFor(model => item.Assigned)
</td>
}
<input type="Submit" value="Select" />
}
我的控制器
[HttpGet]
public ActionResult CAssigned()
{
var orders = db.Locations.ToList();
return View(orders);
}
[HttpPost]
public ActionResult CAssigned(IEnumerable<Location>)
{
return View();
}
html查看源代码
<input data-val="true" data-val-number="The field LocationId must be a number." data-val-required="The LocationId field is required." id="item_LocationId" name="item.LocationId" type="hidden" value="1" />
<td>
red-dot.png
<input Length="8" id="item_MapIcons" name="item.MapIcons" type="hidden" value="red-dot.png" /></td>
<td>
<input class="check-box" data-val="true" data-val-required="The Assigned to Courier field is required." id="item_Assigned" name="item.Assigned" type="checkbox" value="true" /><input name="item.Assigned" type="hidden" value="false" />
<input type="Submit" value="Select" />
<input id="item_Assigned" name="item.Assigned" type="hidden" value="False" />
<td>
您对foreach循环的使用不会生成绑定到集合所必需的正确名称属性,并且您需要对typeof Location
使用for
循环或自定义EditorTemplate
,以便表单控件包括索引器。 请参阅此答案,以获取有关如何需要命名集合的表单控件以及使用EditorTemplate
更多详细信息。
要使用for循环,您需要查看
@model IList<yourAssembly.Location>
@using (Html.BeginForm()) // no need to add the parameters since you posting to the same method
{
<table>
for(int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(m => m[i].LocationId)
@Html.HiddenFor(m => m[i].MapIcons)
@Html.DisplayFor(m => m[i].MapIcons)
</td>
<td>
@Html.CheckBoxFor(m => m[i].Assigned)
</td>
</tr>
}
</table>
<input type="Submit" value="Select" />
}
并且您发布方法签名必须是(为参数添加名称)
[HttpPost]
public ActionResult CAssigned(IEnumerable<Location> model)
旁注:
Assigned
属性隐藏的输入。 您已经为该属性生成了一个复选框(以及相关的隐藏输入),因此无论如何在回发时都将其忽略 <input>
不是<tr>
元素的有效子元素,因此请确保将它们包含在<td>
元素内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.