Saturday, 17 October 2009

Playing with Asp.Net MVC Preview 2

Having played with xVal with MVC 1 I was interested to see the built in client side validation added to the MVC2.

After a little bit of searching and playing around I got the page set up.

 

   1: <script type="text/javascript" src="../../Scripts/jquery-1.3.2.js"></script>
   1:  
   2: <script type="text/javascript" src="../../Scripts/jquery.validate.js">
   1: </script>
   2: <script type="text/javascript" src="../../Scripts/MicrosoftMvcJQueryValidation.js">
</script>



   2:  



   3:     <h2><%
   1: = Html.Encode(ViewData["Message"]) 
%></h2>



   4:     <p>



   5:         To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.



   6:         <%
   1: = Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") 
%>



   7:         <%
   1:  
   2:             Html.ClientValidationEnabled = true; 
%>



   8:         <%
   1: using (Html.BeginForm())
   2:           {
%>



   9:           <%
   1: =Html.EditorForModel() 
%>



  10:            <input type="submit" value="Save" />



  11:         <%
   1: } 
%>



  12:     </p>






Using this simple class as the model.





   1: using System.ComponentModel.DataAnnotations;



   2:  



   3: namespace MvcApplication6.Models



   4: {



   5:     public class Address



   6:     {



   7:         [Required(ErrorMessage = "The House Number or Name field is required")]



   8:         public string HouseNumberName { get; set; }



   9:         [Required]



  10:         public string Street { get; set; }



  11:  



  12:         [Required]



  13:         public string City { get; set; }



  14:  



  15:         [Required]



  16:         public string PostCode { get; set; }



  17:     }



  18: }





Gave me this result.



Validation



I’m not sure about having the <%  Html.ClientValidationEnabled = true; %> line, it doesn’t feel right.  I’m also not sure about the usefulness of Html.EditorForModel() I need to poke around a bit more and see if there is some way of changing the captions.



Apart from that it seems pretty simple.  I would like to see if it can be hooked up to nHibernate validation.



Technorati Tags:

No comments:

Post a Comment