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

时间:2011-09-04 07:04:47

标签: 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;}
    ........
}

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

看起来这很简单,也许我错过了什么。

谢谢!

** **编辑

好的,所以我使用了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.

这是我的控制器动作:

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

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

我有点困惑。

3 个答案:

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

然后定义将为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>

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

[HttpPost]
public ActionResult Edit(AddEditChildProductModel model)
{
    ...
}

答案 1 :(得分:6)

您可以在Phil Haack's article "Model Binding To A List"中查看有关将实体的集合绑定到视图的有用信息。

@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)
{
}

答案 2 :(得分:1)

您也可以在http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style/找到Steven Sanderson提供的解决方案。帮助了我很多。