【问题标题】:DefaultModelBinder Implementation does not populate the model - ASP.NET MVC 2DefaultModelBinder 实现不填充模型 - ASP.NET MVC 2
【发布时间】:2011-08-30 00:51:42
【问题描述】:

我有一个非常简单的 DefaultModelBinder 实现,我需要它来触发一些自定义验证。

public class MyViewModelBinder : DefaultModelBinder 
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ModelStateDictionary modelState = bindingContext.ModelState;
        var model = (MyViewModel)base.BindModel(controllerContext, bindingContext);

        var result = ValidationFactory.ForObject<MyViewModel>().Validate(model);

        CustomValidation(result, modelState);

        return model;
    }
}

MyViewModel 是一个公开的密封类。 模型绑定器以这种方式在 Global.asax 中注册:

ModelBinders.Binders.Add(typeof(MyViewModel), new MyViewModelBinder());

问题是模型永远不会被填充!但是 MVC 默认模型绑定器(我删除了 global.asax 中的注册)工作正常。

这是视图 HTML:

    <table>
        <tr>
            <td><label for="Name">Name</label></td>
            <td><input id="Name" name="Name" type="text" value="" /></td>
        </tr>
        <tr>
            <td><label for="Code">Code</label></td>
            <td><input id="Code" name="Code" type="text" value="" /></td>
        </tr>
    </table> </div>

每个字段都匹配模型的一个属性。

【问题讨论】:

    标签: asp.net-mvc-2 defaultmodelbinder


    【解决方案1】:

    根据您提供的信息,我无法重现该问题。这就是我所做的。

    查看模型:

    public sealed class MyViewModel
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            // at this stage the model is populated perfectly fine
            return View();
        }
    }
    

    索引视图:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <% using (Html.BeginForm()) { %>
            <table>
                <tr>
                    <td><label for="Name">Name</label></td>
                    <td><input id="Name" name="Name" type="text" value="" /></td>
                </tr>
                <tr>
                    <td><label for="Code">Code</label></td>
                    <td><input id="Code" name="Code" type="text" value="" /></td>
                </tr>
            </table>
            <input type="submit" value="OK" />
        <% } %>
    </body>
    </html>
    

    模型绑定器:

    public class MyViewModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (MyViewModel)base.BindModel(controllerContext, bindingContext);
    
            // at this stage the model is populated perfectly fine
            return model;
        }
    }
    

    所以现在的问题是,您的代码与我的代码有何不同?CustomValidationValidate 方法中的代码是什么?

    【讨论】:

    • 你是对的。这是正确的做法。它引导我找到解决方案:问题出在 MyViewModel 的属性中引发异常。谢谢老哥!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多