What's New in ASP.Net MVC 3 Controllers: Part 2


Introduction

The primary goal of the Model-View-Controller (MVC) pattern is to separate as neatly as possible the code that generates the graphical interface displayed to users from the code that manages any user actions. The controller is a component that deals with the performance of any business-related tasks triggered within the page. A controller is invoked in response to some user action and likely needs some input data to do its job. This article is a continuation of the previous article ( http://www.c-sharpcorner.com/UploadFile/sanks/7532/  ). In this article we are going to explore ViewBag and Request Validation - AllowHTML.

Request Validation - AllowHtml

The ValidateInput Attribute marks an action method as one whose posted input data might (or might not) need validation. As shown below the EditProduct action is decorated with the attribute ValidateInput (True) which does not allow posting a form. But the limitation of that attribute is that it blocks posting for a form even if one field has HTML tags. In MVC 3, the System.Web.Mvc.AllowHtml attribute can be decorated for a property of a class which allows HTML tags in the posted data only for a specific property.

MVC2.1.gif

Let's see a step by step approach.

Step 1: Create Product model with ProductName and ProductDescription as property.

MVC2.2.gif

Step 2: In the home controller, create action EditProduct for HttpGet and HttpPost and decorate with attribute ValidateInput (True) for HttpPost as shown below.

MVC2.3.gif

Step 3: Create EditProduct View for Product model with strongly typed and select Sacffold template as Edit.

MVC2.4.gif

Step 4: Run the application and in the Edit Product View, put HTML tags in ProductName.

MVC2.5.gif

Step 5: After submit you will get following validation exception for ProductName.

MVC2.6.gif

Step 6: Now in the Edit Product View, put html tags in ProductDescription.

MVC2.7.gif

Step 7: After submit you will get following validation exception for ProductDescription.

MVC2.8.gif

So in above steps we have seen that the form is validated even if either of the form fields has HTML tags. Now if we want to allow HTML tags for ProductDescription only, then in that case remove the ValidateInput attribute from HttpPost.

MVC2.9.gif

Step 8: In the Product model, add the attribute AllowHtml for the ProductDescription property.

MVC2.10.gif

Step 9: Run the application and in the Edit Product View, put HTML tags in ProductName.

MVC2.11.gif

Step 10: After submit you will get following validation exception for ProductName:

MVC2.12.gif

Step 11: Now in the Edit Product View, put HTML tags in the ProductDescription the form allows for submission.

MVC2.13.gif

ViewData and ViewBag

The ViewData property represents a built-in container used for passing data between a controller and a view. The property is of type ViewDataDictionary. It's a plain .NET class that implements the IDictionary interface and looks and behaves like a classic name/value pair, enumerable dictionary.

The ViewData property is defined on the ControllerBase class to make it available to any custom controllers you might have. The idea is that once the controller has executed a given action, it packs any significant values into the ViewData container to make it flow all the way through the view.

ViewBag, dynamic type, property introduced in MVC 3 is property for controllers and views.

ViewBag and ViewData both work against the same collection of name-value pairs.


Step 1: In the controller action, add the new item "Message" to ViewData dictionary.

MVC2.14.gif

Step 2: In the view, get the ViewData "Message" dictionary using ViewBag.

MVC2.15.gif

Step 3: Run the application to see the output.

MVC2.16.gif

Conclusion: In this article, we have explored Validation Request and ViewBag features of ASP.Net MVC 3. In the forthcoming article, we will explore other features like ActionResults, and Global Action Filters.
 


Similar Articles