Thursday, December 17, 2009

ASP.NET MVC Pattern – VAR Controllers(View-Ajax-Redirect)

In this blog, I am going to introduce one controller pattern, VAR Pattern, for the ASP.NET MVC framework.


For last few days, I have started to learn the ASP.NET MVC framework. I have downloaded and studied various sample/demo applications with help of search engine.

I have also noticed that different operations,like CRUD (Create, Retrieve, Update, Delete), are divided in different views (actually pages). But it may be required that an application needs a single page has all CRUD plus others.

Before going to explain the VAR pattern, we can still develop very good ASP.NET MVC applications using different tools/libraries like Spark view engine, AutoMapper etc and different patterns.

Generally we can categorized 3 types of actions"

1. View - Submit (Postback) – submit the view.
2. Ajax called by client side to update a view through javascript or jquery.
3. Redirect redirects the view.

For each type of action, there is one controller is defined which are listed;

1.ViewController:


It should return View or PartialView.

There are 2 kind of usage,

a). To Submit the view and do the needful operations

e.g. Insert or Delete a record and display other view.


View → Insert is button pressed → “Insert” action is executed from “ViewController” → form data are saved → redirect to Home view


b). To update the view.

e.g. Search operation changes the view data

View → Search is button pressed → “Search” action is executed from “ViewController” → data are fetched based on search criteria → update the view

2.AjaxController:

It should return .NET primitive datatype (converted in Content by ASP.NET MVC framework), ContentResult, JSON result. i.e return type which can be processed/manipulated through javascript.

There is one kind of usage,

a). To fetch data from server and use it at client side.

e.g. In “Registration” view to check that Username is exist or not
“Update” the view data.

View → Modify the form data → Update is button pressed → “Update” action is executed, through jquery ajax, from “AjaxController” → data are updated → notifies the client.

Generally I like to post updated data on server asynchronously if update is not too much complex.
The reason is whatever updated values are known and persist in the view. So It is not required to update the view forcefully through post back.

3.RedirectController:

There are 2 kind of usage.

a). To redirect from one view to another

e.g. Home view to Admin view

Home view → Admin button is pressed → ”RedirectCMS” action is executed from “RedirectController”→ validate the admin token → redirect to Admin view

b). To submit the current view and redirect that view with default values.

e.g. Delete record with current form data and update the view with default values
View → Delete button is pressed → “Delete” action is executed from “RedirectController” → data are deleted → redirects the view with default data.

We can create the VAR controllers for each entity.

I have created the demo (another demo in ASP.NET MVC framework ;)) at http://cid-c91ae1db65f80fc6.skydrive.live.com/self.aspx/.Public/Blog/MVCPattern%5E_VAR.zip