我以一个网格基本html表的形式显示了一个数据列表,并放置了文本框,这样我们就可以在网格内编辑并发布值,以便保存它。这个列表不是很大,大约有5-10行。
如何在控制器中访问这些表单值?FormCollection似乎不起作用,我甚至无法通过Request.Form[]访问这些值。我希望它以列表的形式返回,这样我就可以循环遍历它并获得新的值。
.cshtml
<form action="/Parameter/StudentWeights" method="post">
<table>
<tr>
<th>
Category
</th>
<th>
CategoryAlias
</th>
<th>
YearCode
</th>
<th>
ClassKto8
</th>
<th>
Class9to12
</th>
<th></th>
</tr>
@foreach(var item in Model.StudentWeights) {
<tr>
<td>
@Html.HiddenFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryAlias)
</td>
<td>
@Html.DisplayFor(modelItem => item.YearCode)
</td>
<td>
@Html.EditorFor(modelItem => item.ClassKto8)
</td>
<td>
@Html.EditorFor(modelItem => item.Class9to12)
</td>
</tr>
}
</table>
<input type="submit" value = "Submit" />
</form>控制器
[HttpPost]
public ActionResult studentWeights(FormCollection collection)
{
try
{
// TODO: Add update logic here
//service.
foreach (var item in collection)
{
int x = item. // i want to loop through it and access the values.
}
}
catch
{
return View();
}
}请帮我得到这些价值。我不想使用JEditable或任何第三方jQuery工具。
是否有任何方法来创建自定义类型并在JavaScript或jQuery中指定值,然后单击按钮,然后将其发送给我的控制器操作?
非常感谢,任何建议都会很有帮助。
发布于 2012-10-11 12:41:25
您需要以不同的方式访问表单集合。表单集合是提交给控制器的值的键值对。格式集合“后值”
[HttpPost]
public ActionResult studentWeights(FormCollection formCollection)
{
foreach (string _formData in formCollection)
{
var x = formCollection[_formData];
}
}下面是几种遍历表单集合http://stack247.wordpress.com/2011/03/20/iterate-through-system-web-mvc-formcollection/的方法
https://stackoverflow.com/questions/12713333
复制相似问题