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 do I edit the collection in the same view?
It would seem this would be very simple, maybe I am missing something.
Thanks!!
**Edit**
OK, so I used EditorTemplates, but now I am getting 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);
}
Shouldn’t this work, as I get the ChildProduct with the related PriceTiers collection and use AutoMapper to map the differences? I maintain hidden fields for the PK and FK fields on the PriceTier.
I am a bit confused.
You could use editor templates:
and then define the editor template which will be rendered for each element of the
PriceTierslist (~/Views/Shared/EditorTemplates/AddEditPriceTierModel.cshtml) – the name and location of the editor template is important. You could also put it in~/Views/SomeController/EditorTemplates/AddEditPriceTierModel.cshtmlif the editor template is specific only for a given controller:and now your POST controller action signature will look like this: