首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在控制器中访问表单值,我试图将整个网格发送回post方法ASP.net MVC

在控制器中访问表单值,我试图将整个网格发送回post方法ASP.net MVC
EN

Stack Overflow用户
提问于 2012-10-03 17:03:37
回答 1查看 2.3K关注 0票数 3

我以一个网格基本html表的形式显示了一个数据列表,并放置了文本框,这样我们就可以在网格内编辑并发布值,以便保存它。这个列表不是很大,大约有5-10行。

如何在控制器中访问这些表单值?FormCollection似乎不起作用,我甚至无法通过Request.Form[]访问这些值。我希望它以列表的形式返回,这样我就可以循环遍历它并获得新的值。

.cshtml

代码语言:javascript
复制
 <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>

控制器

代码语言:javascript
复制
[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中指定值,然后单击按钮,然后将其发送给我的控制器操作?

非常感谢,任何建议都会很有帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-11 12:41:25

您需要以不同的方式访问表单集合。表单集合是提交给控制器的值的键值对。格式集合“后值”

代码语言:javascript
复制
[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/的方法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12713333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档