DefaultModelBinder 实现不会填充模型 - ASP.NET MVC 2

发布于 2024-12-01 16:59:27 字数 1325 浏览 1 评论 0原文

我有一个非常简单的 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>

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

I have a very simple implementation of the DefaultModelBinder, I need it to fire some custom validation.

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 is a public sealed class.
The model binder is registered in the Global.asax this way:

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

The problem is that the model is never populated! But the MVC default model binder (I remove the registration in global.asax) works fine.

This is the view 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>

Every field matches a property of the model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-12-08 16:59:27

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

视图模型:

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 方法中的内容是什么?

From the information you provided I am unable to reproduce the problem. Here's what I did.

View model:

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

Controller:

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

Index 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>

Model binder:

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

So now the question is, how does your code differs than mine and what is it in those CustomValidation and Validate methods?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文