【发布时间】:2012-08-06 17:19:33
【问题描述】:
我正在使用http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style 此处的指南,并且正在使用 MVC2。
我已经将它与控制器方法一起使用:
[HttpPost]
public ActionResult CreateStockRequest(StockRequestModel viewModel, List<StockRequestModel.StockRequestItem> items)
{
viewModel.Items = items;
// Validate the request and submit it
return View(viewModel);
}
如您所见,即使我的模型包含 Items 方法,我也必须添加一个 items 参数,因为模型上的属性没有被填充。
我尝试在BeginCollectionItem 方法中将items 更改为Items,并尝试了各种其他值,但如果不在控制器方法中添加单独的items 参数,我无法使其工作。
tl;dr:如何从视图中添加/删除/编辑模型的列表属性中的项目?
查看
<table>
<thead>
<tr>
<td><%= Html.LabelFor(m => m.Items[0].Item )%></td>
<td><%= Html.LabelFor(m => m.Items[0].Quantity )%></td>
</tr>
</thead>
<tbody id="editorRows">
<% foreach (var item in Model.Items)
{
Html.RenderPartial("StockRequestItemEditor", item);
}%>
</tbody>
<tfoot>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2"><%= Html.ActionLink("Add Item...", "BlankEditorRow", null, new { id = "addItem" })%></td>
<script type="text/javascript">
$("#addItem").click(function() {
$.ajax({
url: this.href,
cache: false,
success: function(html) { $("#editorRows").append(html); }
});
return false;
});
</script>
</tr>
</tfoot>
</table>
局部视图
<tr>
<% using(Html.BeginCollectionItem("Items")) { %>
<td>
<%= Html.ComboBoxFor(m => m.Item,
null,
Url.Action("Products", "Data", new { area = (string)null }),
Model.Item,
2)%>
</td>
<td><%= Html.TextBoxFor(m => m.Quantity)%></td>
<% } %>
</tr>
【问题讨论】:
-
更新了视图和局部视图
标签: c# asp.net-mvc asp.net-mvc-2