When we talk about web application reusability is the key. So as a MVC developer we would like to create reusable views. For instance we would like to create reusable views like footer and header views and use them inside one big MVC view.

Reusable views can be achieved by creating Ã¢â‚¬Å“Partial viewsÃ¢â‚¬Â.

The first step would be to create a simple view with a controller. You can see from the below snapshot, I have created a simple view called as Ã¢â‚¬Å“Index.aspxÃ¢â‚¬Â which will be invoked via Ã¢â‚¬Å“Homecontroller.csÃ¢â‚¬Â.

In case you are coming to this section directly please see the previous Labs to synch up.

Now that we have created the main view itÃ¢â‚¬â„¢s time to create a partial view which can be consumed inside the Ã¢â‚¬Å“IndexÃ¢â‚¬Â view. In order to create a partial view , right click on the view folder and mark the check box Ã¢â‚¬Å“Create a partial viewÃ¢â‚¬Â as shown in the below figure.

Finally call the partial view in the main view using Ã¢â‚¬Å“Html.RenderPartialÃ¢â‚¬Â function and pass the view name in the function as shown in the below code snippet.

Also ensure that the partial view is in the same folder where your main view is. In case itÃ¢â‚¬â„¢s not then you need to also pass the path in the Ã¢â‚¬Å“RenderPartialÃ¢â‚¬Â function. You can see in the below figure I have moved the partial view in the main Ã¢â‚¬Å“ViewsÃ¢â‚¬Â folder.

One more thing which is noticeable is that the icons for main view and partial are very different. You can see the yellow border in the partial view icon which does not exist in the main view icon.

Validating data is one of the key things in any web application. As a developer you would like to run validation both on the client side (browser) and also on the server side.

So you would probably like to write the validation once and then expect the validation framework to generate the validation logic on both the ends.

Good news, this is possibleby using data annotations.

In MVC you validate model values. So once the data comes inside the model you would like to question the model saying, is the data providedproper ?, are values in range ?etc.

Data annotations are nothing but the metadata which you can apply on the model and the MVC framework will validate using the metadata provided.

In this lab letÃ¢â‚¬â„¢s enforce validation by using data annotation. So the first thing is use Lab 4 and create a simple model and a strong typed data entry view. In case you have come to this lab straight forward, please once have a look at
day 1 labs, before proceeding ahead.

So assuming you have created the model and the strong typed view, letÃ¢â‚¬â„¢s start applying data annotations.

Import the data annotation namespace as shown in the code snippet below.

using System.ComponentModel.DataAnnotations;

Let say we have a customer model and we want to ensure that the customer code field is compulsory. So you can apply attribute Ã¢â‚¬Å“RequiredÃ¢â‚¬Â as shown in the below code snippet. If the validation fails and you would like to display some error message, you can pass the Ã¢â‚¬Å“ErrorMessageÃ¢â‚¬Â also.

Now there are some code changes we would be doing in the ASPX code as compared to our previous lab. Inside the body we would like to display error message if the data is not proper. This is done by using the below code snippet.

We also need to code our HTML form to input data. Below is the code snippet for the same. Please note the Ã¢â‚¬Å“EditorForModelÃ¢â‚¬Â function will automatically generate UI controls looking at the model properties. So we do not need to create control individually as we did for Lab 4.

As said previously we would like to fire validation on both server and client side. In order to fire validations on the client side, we need to refer three JavaScript files as shown in the below code snippet.

Also note the call to Ã¢â‚¬Å“EnableClientValidationÃ¢â‚¬Â method due to which client side validations are enabled.

From the UI when the form calls a post on the controller, you would like to know if the model state is proper or not. This can be done by checking the Ã¢â‚¬Å“ModelState.IsValidÃ¢â‚¬Â property. So if this property is valid then call the save method and call the thanks view or else go back to the customer view.

Till now this article was using MVC 2 but itÃ¢â‚¬â„¢s high time we also start discussing and doing labs with new release versions of MVC frameworks. Change is a part of human life and same stands true for MVC as well
J. So in this section letÃ¢â‚¬â„¢s discuss MVC 3 which was the next release version after MVC 2.

FYI: - The recent version is MVC4 and in the later days I will be touch basing even those versions. So control yourself and have patience.

In case you are feeling that whatever we have learnt in MVC 2, is it a waste? No, not at all. On the contrary MVC 3 is backward compatible with MVC 2. So whatever you have learnt in MVC2 still holds true in MVC 3.

Now rather than discussing about all the new features letÃ¢â‚¬â„¢s focus on the biggest feature of MVC3 which I personally think is a game changer and that is RAZOR.

So whatÃ¢â‚¬â„¢s Razor, just to answer short and sweet, itÃ¢â‚¬â„¢s a type of view for MVC. In MVC 2 the default view was ASP.NET pages i.e. Web form view. Now the problem of web form views was that it was not made thinking MVC in mind, so the syntaxes was bit heavy.

Developers demanded for a clean, light weight view and with less syntactic noise: - Answer was RAZOR.

So letÃ¢â‚¬â„¢s create a simple lab to demonstrate use of Razor views.

The next screen which pops up what kind of application you want to create.

The Empty option creates a project with least amount of files.

The Internet Application option creates a shell application which includes user registration and authentication, navigation, and a consistent visual style.

The Intranet Application option is very much same as Internet Application with the only difference that the authentication takes place through domain/Active Directory infrastructure.

For now letÃ¢â‚¬â„¢s keep life simple and letÃ¢â‚¬â„¢s select the empty option. The second thing which we need to select is what kind of view we want, so letÃ¢â‚¬â„¢s select Razor and move ahead.

Once the project is created you can see the Razor file with the name Ã¢â‚¬Å“.cshtmlÃ¢â‚¬Â. Now the Ã¢â‚¬Å“_ViewStartÃ¢â‚¬Â page is nothing but itÃ¢â‚¬â„¢s a common page which will be used by views for common things like layouting and common code.

Now go ahead and add a new view and invoke this view from the controller. Adding and invoking the view from controller remains same as discussed in the previous labs. Just remember to select the view as Razor view.

If you want to just display a simple variable you can do something as shown below. All razor syntaxes start with @. If you have just single line of code you do not need Ã¢â‚¬Å“{Ã¢â‚¬Å“. Razor figures out the ending logically.

Todays date @DateTime.Now

If you compare the above syntax with ASPX view you need to type the below code, so isnÃ¢â‚¬â„¢t the syntax much simpler, neat and light weight.

If you are thinking will razor confuse with @ of Razor and @ of your email address, do not worry razor understands the difference. For instance in the below line the first line razor will execute as a code and the second line of code he understands itÃ¢â‚¬â„¢s just an email address.

If you execute the above code you would be surprised to see that it does not display as HTML but as a simple display as shown below. Now thatÃ¢â‚¬â„¢s not what we expect? , we were expecting a proper HTML display. This is done by razor to avoid XSS attacks (I will discuss about the same in later sections).

But no worries razor team has taken care of it. You can use the Ã¢â‚¬Å“Html.RawÃ¢â‚¬Â to display the same as shown in the below code snippet.

Security is one of the most important things in any application irrespective you develop them in any technology, same holds true from MVC. In this lab we will try to implement windows authentication in MVC 3 application.

Now one way to implement windows authentication is by creating project using the intranet application option. As said previously intranet application option is enabled to authenticate users from windows active directory.

For now we will not use that option, letÃ¢â‚¬â„¢s use the empty application option and create from scratch so that we can understand better.

Now itÃ¢â‚¬â„¢s times to go and publish this solution on IIS so that we can test if windows authentication works. In order to do the same we need to have the necessary MVC DLLÃ¢â‚¬â„¢s also posted to the server. So right click on the project and select Ã¢â‚¬Å“Add deployable dependenciesÃ¢â‚¬Â.

In the next screen its will prompt which dependencies you want to include. For now I have razor view so I have selected both the options.

Once you can see the dependent DLLÃ¢â‚¬â„¢s been added to the project.

Once you have created the IIS application, itÃ¢â‚¬â„¢s time to publish your application to the web application folder. So click on build and publish as shown in the below figure. I have used Ã¢â‚¬Å“File systemÃ¢â‚¬Â as the publish method, you can use your choice.