I have a simple question.
I have a model that looks like this:
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;} ........ }
My question is: how to edit a collection in the same view?
It would seem that it would be very simple, maybe I'm missing something.
Thanks!!
** Edit **
OK, so I used EditorTemplates, but now I get the following error:
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.
This is my controller action:
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); }
Should this not work when I get ChildProduct with the associated PriceTiers collection and use AutoMapper to match the differences? I support hidden fields for PK and FK fields on PriceTier .
I am a bit confused.
source share