我有一个简单的问题。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