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://learn.tech-web.vn/article/asp-net-mvc/What-is-New-in-ASP-Net-MVC-3-Controllers-Part-1.aspx ). 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.
Let's see a step by step approach.
Step 1: Create Product model with ProductName and ProductDescription as property.
Step 2: In the home controller, create action EditProduct for HttpGet and HttpPost and decorate with attribute ValidateInput (True) for HttpPost as shown below.
Step 3: Create EditProduct View for Product model with strongly typed and select Sacffold template as Edit.
Step 4: Run the application and in the Edit Product View, put HTML tags in ProductName.
Step 5: After submit you will get following validation exception for ProductName.
Step 6: Now in the Edit Product View, put html tags in ProductDescription.
Step 7: After submit you will get following validation exception for ProductDescription.
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.
Step 8: In the Product model, add the attribute AllowHtml for the ProductDescription property.
Step 9: Run the application and in the Edit Product View, put HTML tags in ProductName.
Step 10: After submit you will get following validation exception for ProductName:
Step 11: Now in the Edit Product View, put HTML tags in the ProductDescription the form allows for submission.
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.
Step 2: In the view, get the ViewData "Message" dictionary using ViewBag.
Step 3: Run the application to see the output.