ASP.NET MVC 3在同一视图中的模型中编辑集合

Sam*_*Sam 4 asp.net-mvc-3

我有一个简单的问题.

我有一个看起来像这样的模型:

public class AddEditChildProductModel
{
    public string Name {get; set;}
    public string Sku {get;set;}
    ........
    public IEnumerable<AddEditPriceTierModel> PriceTiers {get;set;}
}
public class AddEditPriceTierModel
{
    public int QtyStart {get;set;}
    public int QtyEnd {get;set;}
    ........
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在同一视图中编辑集合?

这似乎很简单,也许我错过了一些东西.

谢谢!!

**编辑**

好的,所以我使用了EditorTemplates,但现在我收到以下错误:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Run Code Online (Sandbox Code Playgroud)

这是我的控制器动作:

public ActionResult EditChildProduct(AddEditChildProductModel model)
        {
            if (!ModelState.IsValid)
                return PartialView("AddEditChildProduct", model);

            ChildProduct childProduct = productService.GetChildProductByID(model.ID);
            AutoMapper.Mapper.Map<AddEditChildProductModel, ChildProduct>(model, childProduct);
            foreach (var tier in childProduct.PriceTiers)
            {
                tier.ChildProduct = childProduct;
            }
            UnitOfWork.Commit();

            return ListChildProducts(model.ProductID);
        }
Run Code Online (Sandbox Code Playgroud)

不应该这样,因为我得到ChildProduct相关的PriceTiers集合并使用AutoMapper来映射差异?我维护着PK和FK字段的隐藏字段PriceTier.

我有点困惑.

Dar*_*rov 7

您可以使用编辑器模板:

@model AddEditChildProductModel
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </div>

    <div>
        @Html.LabelFor(x => x.Sku)
        @Html.EditorFor(x => x.Sku)
        @Html.ValidationMessageFor(x => x.Sku)
    </div>

    <table>
        <thead>
            <tr>
                <th>QtyStart</th>
                <th>QtyEnd</th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorFor(x => x.PriceTiers)
        </tbody>
    </table>

    <input type="submit" value="OK">
}
Run Code Online (Sandbox Code Playgroud)

然后定义将为PriceTiers列表的每个元素呈现的编辑器模板(~/Views/Shared/EditorTemplates/AddEditPriceTierModel.cshtml) - 编辑器模板的名称和位置很重要.~/Views/SomeController/EditorTemplates/AddEditPriceTierModel.cshtml如果编辑器模板仅适用于给定的控制器,您也可以将其放入:

@model AddEditPriceTierModel
<tr>
    <td>
        @Html.LabelFor(x => x.QtyStart)
        @Html.EditorFor(x => x.QtyStart)
        @Html.ValidationMessageFor(x => x.QtyStart)
    </td>
    <td>
        @Html.LabelFor(x => x.QtyEnd)
        @Html.EditorFor(x => x.QtyEnd)
        @Html.ValidationMessageFor(x => x.QtyEnd)
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

现在你的POST控制器动作签名将如下所示:

[HttpPost]
public ActionResult Edit(AddEditChildProductModel model)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*ich 6

您可以在Phil Haack的文章"模型绑定到列表"中获取有关将实体绑定到视图的有用信息.

@for (int i = 0; i < Model.MyCollection.Count; i++)
{                    
   @Html.TextBoxFor(m => m[i].Title) 
   @Html.TextBoxFor(m => m[i].Author) 
   @Html.TextBoxFor(m => m[i].DatePublished) 
}


public ActionResult UpdateStuff(MyViewModel vm)
{
}
Run Code Online (Sandbox Code Playgroud)