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.

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.