If you have worked with MVC and started to move towards MVC2, you know how sweet the validation is.
In the past you have to write your custom validation logic and it was clunky and almost hacky. for example if you have a class like this
1: public class ContactUsEntity
2: {
3: public string FirstName { get; set; }
4: public string LastName { get; set; }
5: public string Email { get; set; }
6: public string Message { get; set; }
7: public DateTime Created { get; set; }
8:
9: }
You will probably change your class like this adding a IsValid or Validate method in the domain manager. In real life you will probably create an interface for it. But I will keep this simple here.
1: public class ContactUsDomainManager
2: {
3: ......
4: public IList GetValidationErrors()
5: {
6: ......
7:
8: If(this.FirstName == string.Empty())
9: validationList.Add("First Name is required");
10:
11: ......
12:
13: return validationList();
14: }
15: }
MVC2 gives you the option to handle your error from the class level.
Now you can refactor the class like this
1: public class ContactUsEntity
2: {
3: [Required]
4: [DisplayName("First Name")]
5: public string FirstName { get; set; }
6:
7: [Required]
8: [DisplayName("Last Name")]
9: public string LastName { get; set; }
10:
11: [Required]
12: [DisplayName("Email")]
13: public string Email { get; set; }
14:
15: public string Phone { get; set; }
16:
17: [Required]
18: [DisplayName("Message")]
19: public string Message { get; set; }
20:
21: public DateTime Created { get; set; }
22:
23: }
In your usercontrol on your page you can as long as you bind it to the ContactUsEntity you can write something similar to the following
1: <% using (Html.BeginForm())
2: { %>
3:
4: <table width="50%" >
5: <tr>
6: <td nowrap >
7: <strong>
8: <%= Html.LabelFor(model => model.FirstName) %>
9:
10: </strong>
11: </td>
12: <td >
13: <%=Html.TextBoxFor(model => model.FirstName)%>
14: <%= Html.ValidationMessageFor(model=>model.FirstName) %>
15: </td>
16: </tr>
17: <tr>
18: <td nowrap >
19: <strong><%= Html.LabelFor(model => model.LastName) %>:</strong>
20: </td>
21: <td >
22: <%=Html.TextBoxFor(model => model.LastName)%>
23: <%= Html.ValidationMessageFor(model=>model.LastName) %>
24: </td>
25: </tr>
26: <tr>
27: <td nowrap >
28: <strong><%= Html.LabelFor(model => model.Phone) %></strong>
29: </td>
30: <td >
31: <%=Html.TextBoxFor(model => model.Phone)%>
32: </td>
33: </tr>
34: <tr>
35: <td nowrap >
36: <strong><%= Html.LabelFor(model => model.Email) %></strong>
37: </td>
38: <td >
39: <%=Html.TextBoxFor(model => model.Email)%>
40: <%= Html.ValidationMessageFor(model=>model.Email) %>
41: </td>
42: </tr>
43: <tr>
44: <td nowrap >
45: <strong><%= Html.LabelFor(model => model.Message) %></strong>
46: </td>
47: <td >
48: <%=Html.TextAreaFor(model => model.Message, new { @rows = "10", @cols = "60" })%>
49: <%= Html.ValidationMessageFor(model=>model.Message) %>
50: <div id="dvHidden" style="visibility: hidden">
51: <%=Html.TextBox("Created",DateTime.Now) %>
52: </div>
53: </td>
54: </tr>
55: <tr>
56: <td>
57: </td>
58: <td>
59: <input type="submit" name="submit" class="button" value="Send Now" />
60: </td>
61: </tr>
62: </table>
63: <%
64: }%>
Now you are all done.
Enjoy.
Category: Information Technology