asp.net-mvcmodel-view-controllerasp.net-mvc-2xvalclient-side-validation

Upgrade MVC1/xVal project to MVC2 and compatibilities with client side validation


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...


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.