Question

I am currently looking here to migrate a project from MVC1 to MVC2. We are using xVal for client side validation.

From what I understand, I only have to remove references to xVal and replace by MVC2 EnableClientSideValidation and it should do the trick. Anything else would be required ?

Also, with xVal could not validate on client side a list of models, is it still the case with MVC2 + ClientSideValidation ? (what I mean with the "list of models" is doing validation on a List<TypeOfModel>)

Example of this is : controller:

    public ActionResult Index()
    {
        Models.Model1 model = new Models.Model1();
        model.Usernames = new List<Models.Model2>();

        model.Usernames.Add(new Models.Model2 {  });
        model.Usernames.Add(new Models.Model2 {  });
        model.Usernames.Add(new Models.Model2 {  });
        model.Usernames.Add(new Models.Model2 {  });

        return View(model);
    }

Model1:

class Model1 {  
public List<Model2> Usernames { get; set; }
}

Model2:

class Model2 {  
[Required]
public string Username { get; set; }
}

View:

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm()) { %> 

        <% foreach (var username in Model.Usernames) { %> 
            <% Html.RenderPartial("View2", username); %>
            <br /><br />
        <% } %>

        <input type="submit" />

    <% } %>

View2:

<%= Html.EditorFor(f => f.Username) %>
<%= Html.ValidationMessageFor(a => a.Username)%>

This is a POC where MVC2 client side validation is failing since it validates by field id...

Was it helpful?

Solution

Been there, done that, got the T-Shirt. And wish I never went there -- MVC2 validation is frankly not on par with xVal once you've got xVal properly implemented. In addition, MVC3 validation is much, much better done. So, if I were jumping off xVal, I would vector towards making it work with MVC3 which is a much different beast.

OTHER TIPS

Though I subscribe somewhat to what Wyatt said here especially the part about MVC3, but I have been using MVC2 client side validation and it has not been a bad experience at all.

To answer your question, you need to do the following: 1. Include MicrosoftAjax.js and MicrosoftMvcValidation.js in your view. 2. Then simply call the helper <% Html.EnableClientValidation(); %>

It should pickup your DataAnnotation model validation rules and give you client side validation without much effort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top