I have three viewmodels to edit the same domain object. I use AutoMapper to map to viewmodels from the domain object.
GET
if (Roles.IsUserInRole("administrators"))
{
viewName = "EditAdmin";
editOrder = Mapper.Map<Order, ViewModels.Order.Admin_ViewModel>(order);
}
else if (Roles.IsUserInRole("administrators"))
{
viewName = "EditTechnician";
editOrder = Mapper.Map<Order, ViewModels.Order.Technician_ViewModel>(order);
}
else if (Roles.IsUserInRole("clients"))
{
viewName = "EditClient";
editOrder = Mapper.Map<Order, ViewModels.Order.Client_ViewModel>(order);
}
RedirectToRoute(viewName, editOrder);
POST Is it possible to use the same method for all viewmodels? Possibly by letting the ViewModels inherit from "ViewModelbase"? Tried this with no success :(
Many thanks in advance!
EDIT: This is what my method looks like now:
public ActionResult EditAdmin(ViewModels.Order.Admin_ViewModel model) {...}
Any ideas?
UPDATE: I wasn't able to understand how to use composition (tried it for a few hours). So I went with this to clean up a bit. These are the handlers for my three viewmodels:
[HttpPost]
[Authorize(Roles = "administrators")]
public ActionResult EditAdmin(Admin_ViewModel model)
{
return SaveViewModel(model);
}
[HttpPost]
[Authorize(Roles = "technicians")]
public ActionResult EditTechnician(Technician_ViewModel model)
{
return SaveViewModel(model);
}
[HttpPost]
[Authorize(Roles = "clients")]
public ActionResult EditClient(Client_ViewModel model)
{
return SaveViewModel(model);
}
SaveViewModel looks like this:
protected ActionResult SaveViewModel(dynamic model)
{ ... }
I don't feel to good about this solution. Could you please give me some pointers?