从已经工作的一个创建了一个新的CustomModelBinder.为什么新的一个永远不会被调用来做任何绑定?

Dou*_*ain 5 c# custom-model-binder asp.net-mvc-3

我可以这样做吗?

[HttpPost]
public ActionResult Index(WizardViewModel wizard, IStepViewModel step)
{
Run Code Online (Sandbox Code Playgroud)

我在global.asax.cs application_start中有以下内容

    ModelBinders.Binders.Add(typeof(IStepViewModel), new StepViewModelBinder());
    ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());
Run Code Online (Sandbox Code Playgroud)

更新

所以,我试着看看有什么不对.这是我的新代码.似乎问题出在这个WizardViewModel和它的绑定器上.什么"告诉"应用程序期望和传入的向导模型?

[HttpPost]
public ActionResult Index(WizardViewModel wizard)
{
Run Code Online (Sandbox Code Playgroud)

我在global.asax.cs application_start中有以下内容

    ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());
Run Code Online (Sandbox Code Playgroud)

完整的活页夹代码

namespace Tangible.Binders
{
    public class StepViewModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var stepTypeValue = bindingContext.ValueProvider.GetValue("StepType");
            var stepType = Type.GetType((string)stepTypeValue.ConvertTo(typeof(string)), true);
            var step = Activator.CreateInstance(stepType);

            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => step, stepType); 
            return step; 
        }
    }

    public class WizardViewModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
                var wizardValue = bindingContext.ValueProvider.GetValue("wizard");
                if (wizardValue != null)
                {
                    var wizardType = Type.GetType((string)wizardValue.ConvertTo(typeof(string)), true);
                    var wizard = Activator.CreateInstance(wizardType);

                    bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => wizard, wizardType);
                    return wizard;
                }
                else
                {
                    var wizard = new Tangible.Models.WizardViewModel();
                    wizard.Initialize();
                    return wizard;
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

arc*_*hil 3

答案很简单——是的!当您拥有将值绑定到参数的自定义逻辑时,您应该这样做。您甚至可以使用ModelBinderAttribute来实现这一点,单独设置每个参数。

    [HttpPost]
    public ActionResult Index([ModelBinder(typeof(WizardViewModelBinder))]WizardViewModel wizard, 
[ModelBinder(typeof(StepViewModelBinder))]IStepViewModel step)
    { }
Run Code Online (Sandbox Code Playgroud)

正如我所见,错误在于您的模型绑定程序代码中。我没有时间检查它,但据我记得,CreateModel模型绑定器使用它来创建模型的实例,然后返回的实例是模型绑定的。因此,重写BindModel而不是CreateModelBindModel. 这绝对有效。

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
{
//your model binding logic here
}
Run Code Online (Sandbox Code Playgroud)