I started working at Dominion Digital a little over a week ago and I have already learned so much. I have tinkered with MVC3 on a very small scale, but nothing compared to what I am doing now. In just the first week I have gaining a solid understanding of the importance of a ViewModel and just how easy it is to add both Client and Server-side validation code to your models. Prior to coming here, I would validate on the client using my own jQuery code, and then validate on the server using attributes in System.ComponentModel.DataAnnotations. Validation has been simplified, see my Model below:
using System.ComponentModel.DataAnnotations;
namespace ViewModelExample.Models
{
public class Person
{
[Required(AllowEmptyStrings=false, ErrorMessage="Please enter your First Name!")]
public string FirstName { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Last Name!")]
public string LastName { get; set; }
[Range(18, 100, ErrorMessage="Your age must be between 18 and 60.")]
public int Age { get; set; }
}
}
Here we are setting three properties FirstName (required), LastName (required), and Age (range between 18 and 100). The ViewModel, which probably not needed at this time but the View could change over time:
using ViewModelExample.Models;
namespace ViewModelExample.ViewModels
{
public class PersonVM
{
public Person Person { get; set; }
}
}
My Action:
[HttpPost]
public ActionResult Save(PersonVM personVM)
{
if (ModelState.IsValid)
{
var person = new Person()
{
FirstName = personVM.Person.FirstName,
LastName = personVM.Person.LastName,
Age = personVM.Person.Age
};
return View(new PersonVM()
{
Person = person
});
}
return View("New");
}
...and the Index View:
@using ViewModelExample.ViewModels
@model PersonVM
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using( Html.BeginForm("Save","Person", FormMethod.Post ) )
{
@Html.ValidationSummary(false)
@Html.LabelFor(p => p.Person.FirstName, "First Name")
@Html.TextBoxFor(p => p.Person.FirstName)
@Html.ValidationMessageFor(p => p.Person.FirstName)
<br />
@Html.LabelFor(p => p.Person.LastName, "Last Name")
@Html.TextBoxFor(p => p.Person.LastName)
@Html.ValidationMessageFor(p => p.Person.LastName)
<br />
@Html.LabelFor(p => p.Person.Age, "Age")
@Html.TextBoxFor(p => p.Person.Age)
@Html.ValidationMessageFor(p => p.Person.Age)
<br />
<input type="submit" value="Save" />
}
Now if you set a break point on the Action method, you clearly see this is posting to the web server on every button click. Now look at how simple it is to add client-side validation. First, go to the NuGet package manager and run the following commands:
Install-Package DataAnnotationsExtensions
Install-Package DataAnnotationsExtensions.MVC3
Now throw the following lines in your _Layout.cshtml file:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
Without anymore work, you have added client-side validation and the best part of it is that all of the rules are written right in the Models themselves.