2011-09-04 55 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) 
{ 
}