我有很多类似的ViewModel:
public class RequestForSalaryVM : StatementViewModel
{
// RequestForSalaryVM properties
}
public class ReliefVM : StatementViewModel
{
// ReliefVM properties
}
以及许多类似的方法:
[HttpPost]
public ActionResult SaveRelief(User currentUser, ReliefVM statement)
{
ReliefVM model = (ReliefVM)SaveModel(currentUser, statement);
if (model == null)
return RedirectToAction("List");
return View("Relief", model);
}
[HttpPost]
public ActionResult SaveRequestForSalary(User currentUser, RequestForSalaryVM statement)
{
RequestForSalaryVM model = (RequestForSalaryVM)SaveModel(currentUser, statement);
if (model == null)
return RedirectToAction("List");
return View("RequestForSalary", model);
}
我希望获得类似于这样的东西:
[HttpPost]
public ActionResult SaveStatement(User currentUser, FormCollection statement, string ViewModelName)
{
Assembly assembly = typeof(SomeKnownType).Assembly;
Type type = assembly.GetType(ViewModelName);
object ViewModel = Activator.CreateInstance(type);
//Fill ViewModel from FormCollection <= how can I use asp.net mvc binding for this?
//I do not want to create their own implementation of asp.net mvc binding
return View(ViewModelName, ViewModel);
}