我有三个视图模型来编辑同一个域对象。我使用 AutoMapper 从域对象映射到视图模型。
得到
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 是否可以对所有视图模型使用相同的方法?可能通过让 ViewModels 从“ViewModelbase”继承?试过这个没有成功:(
提前谢谢了!
编辑:这就是我的方法现在的样子:
public ActionResult EditAdmin(ViewModels.Order.Admin_ViewModel model) {...}
有任何想法吗?
更新: 我无法理解如何使用组合(尝试了几个小时)。所以我用这个来清理一下。这些是我的三个视图模型的处理程序:
[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 看起来像这样:
protected ActionResult SaveViewModel(dynamic model)
{ ... }
我对这个解决方案感觉不太好。你能给我一些指点吗?