How to pass data from controllers to views in ASP.NET MVC ? (Tutorial No: 2)

Questpond
Posted by in ASP.NET MVC category on for Advance level | Points: 250 | Views : 148851 red flag
Rating: 4.67 out of 5  
 6 vote(s)

In the previous ASP.NET MVC tutorial we saw how we can create a simple ASP.NET MVC web application. In case you have missed the same do refer ASP.NET MVC tutorials. In this session we will see how we can pass data from controller to views using view data.

How to pass data from controllers to views in ASP.NET MVC ? (Tutorial No: 2)


Introduction

In the part 1 of ASP.NET MVC tutorial we saw how we can create a simple ASP.NET MVC web application. In case you have missed the same do refer ASP.NET MVC tutorials. In this session we will see how we can pass data from controller to views using view data.

Click here for the Part 3 of ASP.NET MVC article.

Click here for the Part 4 of the ASP.NET MVC article.

Click here for the Part 5 of the ASP.NET MVC article.

Click here for the Part 6 of the ASP.NET MVC article.

Click here for the Part 7 of the ASP.NET MVC article.

Click here for the Part 8 of the ASP.NET MVC article.

Click here for the Part 9 of the ASP.NET MVC article.

Click to get more ASP.NET interview question stuffs here.

Lab2:- Passing data between controllers and views

The controller gets the first hit and loads the model. Most of the time we would like to pass the model to the view for display purpose.

As an ASP.NET developer your choice would be to use session variables, view state or some other ASP.NET session management object.

The problem with using ASP.NET session or view state object is the scope. ASP.NET session objects have session scope and view state has page scope. For MVC we would like to see scope limited to controller and the view. In other words we would like to maintain data when the hit comes to controller and reaches the view and after that the scope of the data should expire.

That’s where the new session management technique has been introduced in ASP.NET framework i.e. ViewData.



Video demonstration for Lab 2

Below is a simple youtube video which demonstrates the lab for view data. In this video we will see how we can share data between controller and the view using view data. So we will create a simple controller, record the current data in a view data variable and then display the same in the view using the percentage tag.



 



Step1:- Create project and set viewdata

So the first step is to create a project and a controller. In the controller set the viewdata variable as shown in the below code snippet and kick of the view.


public class DisplayTimeController : Controller
{
//
// GET: /DisplayTime/

public ActionResult Index()
{
ViewData["CurrentTime"] = DateTime.Now.ToString();
return View();
}

}


Step 2:- Display view data in the view.

The next thing is to display the data in the view by using the percentage tag. One important point to note is the view does not have a behind code. So to display the view we need to use the <%: tag as shown in the below code snippet to display the data.

<body>
<div>
<%: ViewData["CurrentTime"] %>
</div>
</body>


So what’s in the next tutorial?

So now that we know how to pass data using view data, the next step is to create a simple model and see all the 3 MVC entities ( i.e. model , view and controller) in action. Get Part 3 of ASP.NET MVC article.

Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Tripati_tutu on: 4/17/2011 | Points: 25
Nice one sir....
Posted by: Akiii on: 4/26/2011 | Points: 25
very good explanation sir........but there is a small mistake........

<%: ViewData["CurrentTime"] %>

here, equalto sign is not given....

<%= ViewData["CurrentTime"] %>

Thanks and Regards
Akiii
Posted by: Questpond on: 4/26/2011 | Points: 25
in asp.net mvc 2 you can use : and =
in mvc 1 you can only use =
Posted by: Ravi_pinnoju on: 4/27/2011 | Points: 25
It is nice one.
Posted by: Skraghava on: 5/30/2011 | Points: 25
Good one Sir.. Hoping to see all 50 articles on MVC so that we can learn them at once.
Posted by: Akiii on: 8/17/2011 | Points: 25
Oh, thank you sir for making me corrected...

Regards
Akiii
Posted by: Vinay13mar on: 11/4/2012 | Points: 25
Hi this is really nice one. check this one more article for passing model value from controller to view

http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=67&title=Binding Google Chart In MVC3 WebGrid Using C#

Login to post response

Comment using Facebook(Author doesn't get notification)