c# - A 从已经工作的一个创建了一个新的 CustomModelBinder。为什么新人永远不会被要求进行任何绑定(bind)?

标签 c# asp.net-mvc-3 custom-model-binder

我可以做这样的事情吗?

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

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

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

更新

所以,我试着看看出了什么问题。这是我的新代码。问题似乎出在这个 WizardViewModel 和它的 Binder 上。什么“告诉”应用程序期望和传入的向导模型?

[HttpPost]
public ActionResult Index(WizardViewModel wizard)
{

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

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

完整的 Binder 代码

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;
                }
        }
    }
}

最佳答案

答案很简单 - 是的!当您拥有用于将值绑定(bind)到参数的自定义逻辑时,这就是您应该做的。您甚至可以使用 ModelBinderAttribute 来做到这一点, 分别设置每个参数。

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

据我所知,错误出在您的模型 Binder 代码中。我没有时间去检查它,但据我所知,CreateModel 被模型绑定(bind)器用来创建模型的实例,然后返回的实例是模型绑定(bind)的。因此,覆盖 BindModel 而不是 CreateModel 并在 BindModel 中编写模型绑定(bind)逻辑。这绝对有效。

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

关于c# - A 从已经工作的一个创建了一个新的 CustomModelBinder。为什么新人永远不会被要求进行任何绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6835964/

相关文章:

c# - 数据缓存的使用

c# - 如何判断文件复制何时结束?

asp.net-mvc-3 - 将类添加到 Razor View 模型中的 Ajax.BeginForm 调用

asp.net-web-api - 如何从 WebAPI 中的自定义绑定(bind)器调用默认模型绑定(bind)?

c# - 属性的自定义模型 Binder

c# - PowerShell 写入输出缺失信息 - 仅打印第一个对象

c# - 如何使用 Java 客户端导入 WCF Web 服务

asp.net-mvc-3 - 从 MVC3 应用程序清除 IIS 缓存

ASP.NET MVC 3.0 FormCollection 中的 JQuery Post 表单

validation - 自定义模型绑定(bind)器中的 DataAnnotations 验证