从Grid MVC提交多行

时间:2013-08-15 20:39:39

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

我试图一次性从简单的网格中添加多个对象。关注文章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");
    }

1 个答案:

答案 0 :(得分: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");
    }