Asp – Prefix in xVal client side causing ModelBinder problems

asp.netasp.net-mvcvalidationxval

Over the last 24 hours, I've become greatly enamored of xVal. I introduced it into my personal web site, and it works like a champ–I can't believe how easy it was to get it going. I'm using ASP.Net MVC, xVal 1.0, and the DataAnnotations validators.

Now trying to put this same code into a work application, I'm seeing different behavior. I'm logical enough to know that I probably have something different, but I can't see what it is.

To make the client side validation work, I've added prefixes "message" to my fields, like so:

<%= Html.TextBox("message.Subject", Model.Subject, new { @class = "contact-input required" })%>

And so on. My Controller then has this logic:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(Message message)
    {
        try
        { // some code here

Now, automagically, the ModelBinder is actually populating my object. This is great.

The problem is that my work project, I'm trying to do the same thing–the clientside validation works great, but the ModelBinder doesn't populate my model. I know it's a prefixing problem, as when I remove the prefix, it's fine.

The only difference in the scripts is that the work project is using the minified jquery validation library.

Any ideas on what to look for?

Best Answer

You don't need any prefixes to make the xVal work, but you do need to the same names for your inputs so that your ModelBinder will work properly. So just add the DataAnnotations to the properties of the classes that you are going to bind to the view and call the xVal html helper "ClientSideValidation", also you need to add the reference to xval, scripts: jquery, jquery.validate, xval.validate ...