2011-09-04 56 views
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;} 
    ........ 
} 

我的问题是我怎么在同一视图编辑的最爱?

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

谢谢!

**编辑**

OK,所以我用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字段的隐藏字段。

我有点困惑。

回答

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) 
{ 
    ... 
} 
+0

你看看我上面的编辑,看看你能帮助我吗?谢谢!! – Sam

+0

@Sam Striano,哦,这似乎是一些EF相关的东西。我不使用EF并且不能帮助您,因为我不是该领域的专家。这个问题不再与ASP.NET MVC 3有关,这是您的问题的主题,但与您正在使用的一些数据访问技术有关。 –

+0

谢谢!你让我指出了正确的方向。 – Sam

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