gpt4 book ai didi

c# - 从 Grid MVC 提交多行

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:20 25 4
gpt4 key购买 nike

我想一次从简单的网格中添加多个对象。已关注文章 http://www.donnfelker.com/editable-grid-list-binding-in-mvc2/但可能遗漏了一些我无法弄清楚的东西。在我的 Controller 中,我在发布项目时得到 null

查看:

    @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>

Controller :

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

最佳答案

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");
}

关于c# - 从 Grid MVC 提交多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com