asp.net mvc从FormCollection填充viewModel

Ily*_*lya 7 c# asp.net asp.net-mvc model-binding

我有很多类似的ViewModel:

public class RequestForSalaryVM : StatementViewModel
{
  // RequestForSalaryVM properties
}

public class ReliefVM : StatementViewModel
{
  // ReliefVM properties
}
Run Code Online (Sandbox Code Playgroud)

和很多类似的方法:

[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);
}
Run Code Online (Sandbox Code Playgroud)

我想得到这样的东西:

[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);
}
Run Code Online (Sandbox Code Playgroud)

Zab*_*sky 8

您可以尝试Controller.UpdateModelController.TryUpdateModel方法:

[HttpPost]
public ActionResult SaveStatement(User currentUser, FormCollection statement, string ViewModelName)
{
    ...
    object ViewModel = Activator.CreateInstance(type);
    if (TryUpdateModel(viewModel))
    {
        // save the ViewModel
    }   

    return View(ViewModelName, ViewModel);
}
Run Code Online (Sandbox Code Playgroud)

但是我建议你创建一个自定义的ModelBinder,因为它有责任创建和填充模型属性.

我可以向您展示如何实现这一目标的简单示例:

基本ViewModel

public abstract class StatementViewModel
{
    public abstract StatementType StatementType { get; }
    ...
}

public enum StatementType
{
    Relief,
    RequestForSalary,
    ...
}
Run Code Online (Sandbox Code Playgroud)

的ViewModels

public class RequestForSalaryVM : StatementViewModel
{
    public override StatementType StatementType
    {
        get { return StatementType.RequestForSalary; }
    }
    ...
}

public class ReliefVM : StatementViewModel
{
    public override StatementType StatementType
    {
        get { return StatementType.Relief; }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

ModelBinder的

public class StatementModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        var statementTypeParameter = bindingContext.ValueProvider.GetValue("StatementType");
        if (statementTypeParameter == null)
            throw new InvalidOperationException("StatementType is not specified");

        StatementType statementType;
        if (!Enum.TryParse(statementTypeParameter.AttemptedValue, true, out statementType))
            throw new InvalidOperationException("Incorrect StatementType"); // not sure about the type of exception

        var model = SomeFactoryHelper.GetStatementByType(statementType); // returns an actual model by StatementType parameter
                                                                         // this could be a simple switch statement
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, model.GetType());
        bindingContext.ModelMetadata.Model = model;
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

之后注册模型绑定器Global.asax:

ModelBinders.Binders.Add(typeof(StatementViewModel), new StatementModelBinder());
Run Code Online (Sandbox Code Playgroud)

调节器

[HttpPost]
public ActionResult Index(StatementViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        // save the model
    }
    return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)