我有一個簡單的問題。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字段的隱藏字段。
我有點困惑。
你看看我上面的編輯,看看你能幫助我嗎?謝謝!! – Sam
@Sam Striano,哦,這似乎是一些EF相關的東西。我不使用EF並且不能幫助您,因爲我不是該領域的專家。這個問題不再與ASP.NET MVC 3有關,這是您的問題的主題,但與您正在使用的一些數據訪問技術有關。 –
謝謝!你讓我指出了正確的方向。 – Sam