Search code examples
asp.net-mvc-2defaultmodelbinder

DefaultModelBinder Implementation does not populate the model - ASP.NET MVC 2


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.


Solution

  • 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?