Computer Software Learning
Computer Software Learning
This section is very important for you because here you will learn various techniques to transfer model data to view pages. Till now, you have learned how to access database value using models and controllers, and this time is to learn how to display those data to view pages. After completing this section, you will be able to design model data to view pages with style.
There are following 9 ways by which you can display model data to view pages.
1. View Model
2. ViewBag, ViewData, and TempData
3. Session, Tuples, and Dynamic
4. Render Action and Navigation Properties
VIEW MODEL
View Model is nothing, just a class that calls all models in a single class and then returns values to view pages. It becomes very useful when you have multiple models and want to display their value in one view pages.
1. CREATE MODELS AND CONTROLLERS
Before Starting this section, you must create following classes.
ItemModels – It retrieves Item Details
BuyersCommentsModel – It retrieves Comments
ItemCommentsController – It will retrieve Items and Comments from models. I have added some items and comments in controller class in order to reduce complexity. Using the same method you can display database value to view pages.
VIEWMODEL
Step 2: Add highlighted (Line: 42-49) code snippets in ItemCommentsController.
Step 3: Right-click on ItemCommentDisplay
Action Method and select Add View…
Step 4: Set the value as this picture below and click Add:
Step 5: Now, go to Views ItemComments ItemCommentDisplay.cshtml
and Add the following code.
Step 6: Press F5 to run this program and navigate to following link to see the output.
In the previous chapter, you learned what is ViewModel and how to use it for sending model data to views. In this chapter, you will learn how to use ViewBag, ViewData and TempData for passing information from controllers/model to views.
Before Starting this section, you must create following model classes and controllers. If you have already created it in the previous chapter then skip creating these models and controllers.
ItemModels – It retrieves Item Details
BuyersCommentsModel – It retrieves Comments
ItemCommentsController – It will retrieve Items and Comments from models. I have added some items and comments in controller class in order to reduce complexity. Using the same method you can display database value to view pages.
ViewBag is a dictionary of objects and it is used for passing data from controllers to view. It stores values in a dynamic string property and you need to also use this string to retrieve values in the view page. ViewBag is used for only one-time request and once it populates the value to views it becomes null. If any redirection occurs, then also ViewBag value becomes null. There is no typecasting needed in ViewBag.
ItemCommentsController.cs
and add the following highlighted action method.ItemCommentDisplay()
Action Method and create a View.
ItemCommentDisplay.cshtml
View Page.OUT IMAGES Given Below
ViewData is almost same as ViewBag and it is also used for passing models/controllers data to view. It stores data in Key-Value pair and it is also available for current request only. Means, if redirection occurs then ViewData value becomes null. You must check for null and typecast complex data type for avoiding errors.
ItemCommentsController.cs
and add the following highlighted action method.ItemCommentsController.cs
and change the ItemCommentDisplay()
action method.
TempData is also used for transferring Models/Controllers data to views but it provides more functionality than ViewBag and ViewData. TempData doesn’t become null when redirection occurs and it is accessible in two continuous requests. However, TempData is mostly used for displaying Error Messages and Validation Messages. You must check for null and typecast complex data type in order to avoid errors.
ItemCommentsController.cs
and add the following highlighted action method.ItemCommentsController.cs
and change the ItemCommentDisplay()
action method.In this chapter, you learned how to pass multiple models data to view using ViewBag, ViewData and TempData. However, there are also various ways to achieve this goal and in the next chapter, you will learn How to use Session to Pass Model to Views.
ASP.NET Session is also used for passing data between models/controllers to the view page but its value doesn’t become null after passing information until you manually set it to null or session times expires. By default, in ASP.NET MVC, session time is 20 minutes and you can increase or decrease time upon your needs. A session is mostly used in login page where a user is considered as validated until the session expires or the user clicks on log out button.
ItemCommentsController.cs
and add the following highlighted action method.ItemCommentDisplay()
Action Method and create a View.ItemCommentDisplay.cshtml
View Page.Tuples are first introduced in .NET Framework 4.0. It is immutable, fixed-size and ordered sequence object. It is a data structure that has a specific number and sequence of elements and it supports up to 7 to 8 elements. You can use Tuples for passing multiple models in a single view page.
ItemCommentsController.cs
and change the ItemCommentDisplay()
action method.Let’s understand this code
As we discussed earlier that tuples supports maximum of 7 to 8 items; so pass the models in Tuple<model1, model2, model3,….model7>
brackets and then pass the respective action method in sequence that is mapped with model, like (function1, function2, function3,….function7)
. Here, I did the same thing. Just look this code below. Here I have passed my models and their respective action method in sequence.
Step 2: Now, add the following code in ItemCommentDisplay.cshtml
View Page.
Let me highlight some point of this code.
1. Add tuples in the beginning of the view page and pass models name in parameter.
2. You don’t need to remember the models name when accessing its member. You need to use@Model.Item1, @Model.Item2, @Model.Item3…. @Model.Item7
for accessing the model. Here,@Model.Item1
represents ItemModel
, and@Model.Item2
represents List<BuyersCommentsModel>
ExpandoObject
can also be used for passing data among Models, Controllers and Views. The member of ExpandoObject can be added or removed at runtime. It is very flexible and dynamic in nature and very useful when you want to display view page dynamically at runtime. We will discuss on Dynamic (ExpandoObject) in details later. Here, just see how to use Dynamic (ExpandoObject) for passing information in MVC.
ItemCommentsController.cs
and change the ItemCommentDisplay()
action method.
ItemCommentDisplay.cshtml
View Page.
In this chapter, you learned how to pass multiple model data to views using Session, Tuples and Dynamic (ExpandoObject) in ASP.NET MVC. I tried to teach you these objects with the help of an easy and complete example. In the next chapter, you will learn how to pass multiple model data to a single view page using Render Action, JSON, and Navigation Property.