在 MVC 3 中编辑多个项目的正确方法是什么?
我有一个基类Product(属性:int ProductID、string Name、decimal Price)和一些基于产品的实例类型,例如具有附加属性的HardDisk。
我尝试在单个视图中进行编辑,该视图具有基于产品列表的模型:
@model List<Product>
此视图有一个编辑器语句:
@Html.EditorForModel(Model)
我为 Product 类创建了一个编辑器模板,它是一个允许用户编辑的 Ajax 表单每个产品的名称:
@model Product
@{
// set unique IDs for result divs
var resultDiv = "result" + Model.ProductID.ToString();
}
@using (Ajax.BeginForm("Update", new AjaxOptions
{
HttpMethod = "POST",
LoadingElementId = "working",
UpdateTargetId = @resultDiv,
InsertionMode = InsertionMode.Replace
}
))
{
<div style="padding: 4px; margin-bottom: 4px; border: 1px solid gray; background-color: #eee;">
@Html.HiddenFor(m => m.ProductID)
@Html.DisplayFor(m => m.Name)
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
<input type="submit" value="Save" />
<span id="@resultDiv">...</span>
</div>
}
这对于第一行(ID [0].Name)效果很好,将通过单个条目发送数据。但是,编辑第二行或第三行会导致帖子上的数据为空。
I have an base class Product (properties: int ProductID, string Name, decimal Price) and some instance types based on product, e.g. HardDisk which have additional properties.
I have experimenting with edits in a single view that has a model based on a list of products:
@model List<Product>
This view has a single editor statement:
@Html.EditorForModel(Model)
I created an editor template for the Product class, which is a single Ajax form to allow a user to edit the name for each product:
@model Product
@{
// set unique IDs for result divs
var resultDiv = "result" + Model.ProductID.ToString();
}
@using (Ajax.BeginForm("Update", new AjaxOptions
{
HttpMethod = "POST",
LoadingElementId = "working",
UpdateTargetId = @resultDiv,
InsertionMode = InsertionMode.Replace
}
))
{
<div style="padding: 4px; margin-bottom: 4px; border: 1px solid gray; background-color: #eee;">
@Html.HiddenFor(m => m.ProductID)
@Html.DisplayFor(m => m.Name)
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
<input type="submit" value="Save" />
<span id="@resultDiv">...</span>
</div>
}
This works fine for the first row (which has ID [0].Name) will send the data with a single entry. However editing a second or third line results in null data on the post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里找出原因和解决方案 - “模型绑定到列表”链接对于阅读很有用。
这里的问题是模型绑定器假设索引将从零开始并继续。每个项目的生成名称遵循以下模式:
但是,因为我使用单个 AJAX 表单来提交每个条目,所以如果我编辑第一个项目 ( [0] ),模型绑定器只能从索引创建有效列表。
如果我编辑第二个项目,则表单帖子只有 [1].ProductId 和 [1].Name 的值 - 因此它不是从零开始的索引 - 因此活页夹无法重新创建产品数据 - 因此空值。
我的解决方案是将 POST 视为真正的内容 - 对单个对象进行单次编辑。为此,我需要将每个控件上的“名称”属性更改为“ProductID”和“名称”,然后修改处理 POST 的 Update 方法以期望单个产品类型。
然后这一切就完美了。
Figured out the cause and solution here - the "Model Binding to A List" link was useful to read.
The problem here is that the model binder assumes an index will start with zero and continue. The generated names for each item follow this pattern:
However, because I was using a single AJAX form to submit for each entry the model binder could only create a valid list from the index if I edited the first item ( [0] ).
If I edited the second item, the form post had just values for [1].ProductId and [1].Name - so it wasn't a zero-based index - so the binder failed to recreate the product data -- hence the null value.
My solution was to treat the POST as what it really is - a single edit on a single object. To do this I needed to change the "name" attribute on each control to just "ProductID" and "Name" and then modified the Update method that handles the POST to expect a single Product type.
This then worked perfectly.
我们需要为每个项目提供一个索引来绑定复杂的对象。
如果您使用模板化助手,请在
Product.ascx 的主视图页面中使用如下所示的模式(部分页面或模板助手)
we need to provide an index for each item to bind complex objects.
If you are using templated helpers,use pattern like below in main viewpage
In Product.ascx (partail page or template helper)
您应该看看以下文章
模型绑定到列表
you should have a look at the following article
Model Binding To A List