O'Reilly logo

.NET Framework 4.5 Expert Programming Cookbook by A.P. Rajshekhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing unobtrusive validation

In previous versions of ASP.NET Web Pages, you had to manually check each field and add the error message to the ModelState class. However, Version 4.5 of .NET (ASP.NET Web Pages Version 2) introduced the Validation helper, which can not only help your server-side validation but also provide unobtrusive client-side validation. In this recipe, we will enhance the Map Display application by adding the required field validation to the address field.

How to do it...

  1. Open the MapDisplay website.
  2. Open DisplayMap.cshtml. Add the following code to the top of the page, above the DOCTYPE section:
    @{
        Validation.RequireField("address", "Address is required");
    }
  3. Next, add the following code to the <head> section:
    <script src="~/Scripts/jquery.validate.js" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required