RSS

Monthly Archives: July 2011

Querying Properties within csproj files using XML

Another short and sweet post.  As part of the project I’m currently working on, I needed to use the value stored in the RootNamespace Node within the Project file (.csproj).

Now as a csproj file is essentially an XML document we can use the XML libraries in Visual studio to obtain the values we are after.

Heres the code I used to return the value from the RootNamespace Element.  (Note: Remember to set the Namespace!!)

 

Querying a csproj file

 

 

Advertisements
 
Leave a comment

Posted by on July 20, 2011 in C#, XML

 

MVC Validation – Part 4 – Styling the Validation Controls

This is going to be a quick post but still worth mentioning.  Out of the box, there are 6 style classes that we can latch onto to style the validation we see on screen.

These are:

  1. .field-validation-error
  2. .field-validation-valid
  3. .input-validation-error
  4. .input-validation-valid
  5. .validation-summary-errors
  6. .validation-summary-valid

If you use the Visual Studio MVC 3 template to create your project you will notice a content folder which contains a style sheet called ‘Site.CSS’ This has a section for styling the validation helpers listed above.

Styles for the Validation Helpers as created in the Site.Css

So as a quick test let’s make some simple changes to the validation summary helper.  In its default state it may
look something like this:

Screen showing the Valdiation Summary

To change the validation summary we must modify the validation-summary-errors style, so lets make a few simple changes.

Modify the style in to look like:

Now re-run the application and force an error.  Your Validation Summary will look similar to this:

Validation Summary with modified style

 

MVC Validation – Part 3 – Client-Side Validation

A great feature about MVC 3 is the ability to swap between Server and Client side validation, easily and painlessly.

In Part 1 and 2, I created a simple Customer Creation form and applied standard and custom validation to the model class.  However, when we run the application you might have noticed a ‘post back’ prior to the validation summary being displayed.   This is because, by default, MVC 3 applies Server-Side validation.

In this article I will switch our Customer creation example to use Client-Side Validation instead.

Step 1 – Turning on Client Side Validation

By default, server-side validation is enabled but we have the ability to change this to client-side on a page-by-page basis or for our whole application.

To turn on Client-Validation for the whole application, open the web.config and add the following key to the appSettings node:

ClientValidationEnabled Key

Note: if I wanted only our Create.cshtml page use client-side validation then I can simply add the EnableClientValidation method to the page e.g.

Step 2 – Validation Summary

If you followed Part 1 then you would have allowed Visual Studio to create a view (Create.cshtml). In this case the ValidationSummary would have been automatically added.

If not ensure to have the ValidationSummary method call after the BeginForm statement e.g.

Validation summary declaration

Step 3 – Jquery & Unobstrusive Javascript

In our example you may have noticed these 2 script references at the top of our create.cshtml page (these are also shown in the screen shot above)

MVC 3 uses JQuery and unobtrusive javascript to provide valdiation (unlike MVC 2) and when constructing the markup. Ensure that these 2 script files are referenced either on your page or in the _Layout.cshtml if you
are using a ‘master’ page.

Also add the following key to the appsettings node in the web.config to ensure we are using unobtrusive javascript:

Its worth mentioning, just for your reference, that MVC 2 used JSON to enforce the validation in the markup once the page was rendered e.g.

MVC 2 used JSON to enforce client-side validation

However as MVC 3 uses Unobtrusive Javascript, the JSON has been replaced with HTML 5 compatible tags which make for much cleaner code e.g.

MVC 3 uses unobtrusive javascript

You can try this for yourself by just setting the UnobtrusiveJavaScriptEnabled key true & false