0

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.

ab_732
  • 3,639
  • 6
  • 45
  • 61

1 Answers1

1

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?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • You are right. That's the right approach. It leaded me to the solution: the problem was in a property of MyViewModel throwing an exception. Thanks mate! – ab_732 Aug 30 '11 at 02:29