【问题标题】:Submit Multiple Rows from Grid MVC从 Grid MVC 提交多行
【发布时间】:2013-08-15 20:39:38
【问题描述】:

我正在尝试一次从简单网格中添加多个对象。关注文章http://www.donnfelker.com/editable-grid-list-binding-in-mvc2/ 但可能错过了一些我无法弄清楚的事情。在我的控制器中,发布的项目为空

查看:

    @model List<ItemClass>
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post))
    {
        <table>
        <thead>
            <th>PropertyA</th>
            <th>PropertyB</th>
        </thead>
    @for (int i = 0; i < Model.Count; i++)
    {
        @(Html.EditorFor(m => Model[i], "TemplateName"))
    }
        </table>
        <button type="submit" value="Submit"></button>
    }

单个项目的编辑模板:

    @model ItemClass
    <tr>
        <td>
           @Html.EditorFor(m => Model.PropertyA)
        </td>
        <td>
           @Html.EditorFor(m => Model.PropertyB)
        </td>
    </tr>

控制器:

    [HttpPost]
    public ActionResult CreateItems(List<ItemClass> items)
    {
        //create in db
        return RedirectToAction("XXXXXX");
    }

【问题讨论】:

  • 位于托管此模板的父视图中。它生成的输出 HTML 具有所需的表格标签
  • 你说得对
  • 。我已经用你的代码进行了测试。它正在发布带有收藏的模型。如果有帮助,我可以发布代码。
  • 想通了 :) 父视图中的集合 (List&lt;ItemClass&gt;) 是我实际使用的模型的属性。更改了CreateItems(List&lt;ItemClass&gt; items) 的参数名称以匹配属性名称CreateItems(List&lt;ItemClass&gt; itemsProperty)。只是没有提到父视图的模型实际上包含列表作为模型的子属性,为简单起见 - 最后结果是罪魁祸首......感谢您让我超越这些代码行:)

标签: c# asp.net asp.net-mvc-3 razor-2


【解决方案1】:
Try this

[HttpPost]
    public ActionResult CreateItems(List<ItemClass> items)
    {


//       Impelement the for loop with the parameter items varialbe to store the values 
  //     in database 

       for(int i= 0 ; i<item.count; i ++)

        {
           //Implement insertion logice here.

        }

        return RedirectToAction("XXXXXX");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多