Created a new CustomModelBinder from an existing one. Why is a new one never called to make a binding?

Can I do something like this?

[HttpPost]
public ActionResult Index(WizardViewModel wizard, IStepViewModel step)
{

Where I have the following in my global.asax.cs application_start

    ModelBinders.Binders.Add(typeof(IStepViewModel), new StepViewModelBinder());
    ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());

Update

So, I tried to understand what was wrong. Here is my new code. It seems that the problem is with this WizardViewModel and its binder. What "reports" the expected application and the inbound Wizard model?

[HttpPost]
public ActionResult Index(WizardViewModel wizard)
{

Where I have the following in my global.asax.cs application_start

    ModelBinders.Binders.Add(typeof(WizardViewModel), new WizardViewModelBinder());

Full binding code

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;
                }
        }
    }
}
+5
source share
4 answers

- ! , , . ModelBinderAttribute, .

    [HttpPost]
    public ActionResult Index([ModelBinder(typeof(WizardViewModelBinder))]WizardViewModel wizard, 
[ModelBinder(typeof(StepViewModelBinder))]IStepViewModel step)
    { }

, , . , , , CreateModel , . , BindModel CreateModel BindModel. .

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
{
//your model binding logic here
}
+3

- , , .

0

, : ! " ", . , , . , , , . , .

0

, ASP.NET MVC Model Binding , .

Since the model binding was not as transparent as I hoped for complex Model / ViewModels, I simply created my own ActionFilter to resolve the types [and ONLY types] that I want to deserialize in the action method and use ServiceStack.Text for all my tasks serialization / deserialization.

Take a look here:

https://gist.github.com/3b18a58922fdd8d5a963

0
source

All Articles