Passing Data from a Controller to a View

Một phần của tài liệu Developing ASP NET MVC web applications INTL (Trang 66 - 74)

In an ASP.NET MVC application, a controller typically performs the business logic of the application and needs to return the result to the user through a view. So, a mechanism is required to pass the data from a controller to a view. You can use the following objects to pass data between controller and view:

ẻ ViewData ẻ ViewBag ẻ TempData

ViewData

You can use a ViewData object pass data from a controller to a view. ViewData is a dictionary of objects that is derived from the ViewDataDictionary class. The objects in ViewData are stored as key-value pairs that are accessible through the keys.

Some of the characteristics of ViewData are as follows:

y The life of a ViewData object exists only during the current request.

y The value of ViewData becomes null if the request is redirected.

y ViewData requires typecasting when you use complex data type to avoid error.

While using ViewData, you can specify values by using key-value pairs in the action method.

Syntax:

ViewData[<key>] = <Value>; where,

Key: Is a String value to identify the object present in ViewData.

Value: Is the object present in ViewData. This object may be a String or a different type, such as DateTime.

Session Views in ASP.NET MVC

3

Concepts

Code Snippet 5 shows a ViewData with two key-value pairs in the Index action method of the HomeController class.

Code Snippet 5:

public class HomeController : Controller { public ActionResult Index() {

ViewData[“Message”] = “Message from ViewData”;

ViewData[“CurrentTime”] = DateTime.Now;

return View();

} }

In this code, a ViewData is created with two key-value pairs. The first key named, Message contains the String value, “Message from ViewData”. The second key named, CurrentTime contains the value, DateTime.Now. Following is the syntax of ViewData:

Syntax:

@ViewData[<key>]

where,

Key: Is the value used to retrieve the corresponding value present in ViewData. Code Snippet 6 shows retrieving the values present in ViewData.

Code Snippet 6:

<!DOCTYPE html>

<html>

<head>

<title>Index View</title>

</head>

<body>

<p>

@ViewData[“Message”]

</p>

<p>

@ViewData[“CurrentTime”]

Session Views in ASP.NET MVC

3

Concepts

</p>

</body>

</html>

In Code Snippet 6, ViewData is used to display the values of the Message and CurrentTime keys.

Figure 3.4 shows the output of the ViewData.

Figure 3.4: Output of ViewData

ViewBag

In the view, you can access and display the ViewBag data. ViewBag is a wrapper around ViewData and similar to ViewData, ViewBag exists only for the current request and becomes null if the request is redirected. However, unlike ViewData, which is a dictionary of key-value pairs, ViewBag is a dynamic property, based on the dynamic features introduced in C# 4.0. In addition, unlike ViewData, ViewBag does not require typecasting when you use complex data type.

Syntax:

ViewBag.<Property> = <Value>; where,

Property: Is a String value that represents a property of ViewBag. Value: Is the value of the property of ViewBag.

Session Views in ASP.NET MVC

3

Concepts

Code Snippet 7 shows a ViewBag with two properties in the Index action method of the HomeController class.

Code Snippet 7:

public class HomeController : Controller {

public ActionResult Index() {

ViewBag.Message = “Message from ViewBag”;

ViewBag.CurrentTime = DateTime.Now;

return View();

} }

In this code, a ViewBag is created with two properties. The first property named, Message contains the String value, “Message from ViewBag”. The second property named, CurrentTime contains the value, DateTime.Now.

Code Snippet 8 shows retrieving the values present in ViewBag. Code Snippet 8:

<!DOCTYPE html>

<html>

<head>

<title>Index View</title>

</head>

<body>

<p>

@ViewBag.Message

</p>

<p>

@ViewBag.CurrentTime

</p>

</body>

</html>

Session Views in ASP.NET MVC

3

Concepts

In this code, ViewBag is used to display the values of the Message and CurrentTime properties.

Figure 3.5 shows the output of ViewBag.

Figure 3.5: Output of ViewBag

When you use a ViewBag to store a property and its value in an action, that property can be accessed by both ViewBag and ViewData in a view.

Code Snippet 9 shows a controller action storing a ViewBag property.

Code Snippet 9:

public class HomeController : Controller {

public ActionResult Index() {

ViewBag.CommonMessage = “Common message accessible to both ViewBag and ViewData”;

return View();

} }

In this code, a ViewBag is created with a property named, CommonMessage.

Session Views in ASP.NET MVC

3

Concepts

Code Snippet 10 shows a view that uses both ViewData and ViewBag to access the CommonMessage property stored in ViewBag.

Code Snippet 10:

<!DOCTYPE html>

<html>

<head>

<title>Index View</title>

</head>

<body>

<p>

<em>Accessed from ViewData:</em> @ViewData[“CommonMessage”]

</p>

<p>

<em>Accessed from ViewBag:</em> @ViewBag.CommonMessage

</p>

</body>

</html>

The code uses both ViewData and ViewBag to display the value of the CommonMessage property stored in ViewBag.

Figure 3.6 shows the output of ViewData and ViewBag.

Figure 3.6: Output of ViewData and ViewBag

Session Views in ASP.NET MVC

3

Concepts

TempData

Another object that you can use to pass data from a controller to a view is TempData. TempData is a Dictionary object derived from the TempDataDictionary class. Similar to ViewData, TempData stores data as key-value pairs. However, unlike ViewData and ViewBag that does not preserve values during request redirections, TempData allows passing data from the current request to the subsequent request during request redirection.

Syntax:

TempData[<Key>] = <Value>; where,

Key: Is a String value to identify the object present in TempData. Value: Is the object present in TempData.

Code Snippet 11 shows how to use TempData to pass values from one view to another view through request redirection.

Code Snippet 11:

public class HomeController : Controller {

public ActionResult Index() {

ViewData[“Message”] = “ViewData Message”;

ViewBag.Message = “ViewBag Message”;

TempData[“Message”] = “TempData Message”;

return Redirect(“Home/About”);

}

public ActionResult About() {

return View();

} }

The code creates two actions, Index and About in the HomeController class. The Index action stores value to ViewData, ViewBag, and TempData objects. The Index action then redirects the request to the About action by calling the Redirect() method. The About action returns the corresponding view, which is the About.cshtml view.

Session Views in ASP.NET MVC

3

Concepts

Code Snippet 12 shows the About.cshtml view.

Code Snippet 12:

<!DOCTYPE html>

<html>

<head>

<title>Index View</title>

</head>

<body>

<p>

<em>Accessed from ViewData:</em> @ViewData[“Message”]

</p>

<p>

<em>Accessed from ViewBag:</em> @ViewBag.Message

</p>

<p>

<em>Accessed from TempData:</em> @TempData[“Message”]

</p>

</body>

</html>

The code shows the About.cshtml view that the About action returns. This view attempts to access the values stored in the ViewData, ViewBag, and TempData objects.

Figure 3.7 shows the output of About.cshtml view.

Figure 3.7: Output of About.cshtml View

Session Views in ASP.NET MVC

3

Concepts

In figure 3.7, note that the values stored in the ViewData and ViewBag objects are not displayed.

This is because both the objects do not preserve values during the request redirection process.

However, as TempData preserves values during request redirections, the value stored in TempData is displayed.

Một phần của tài liệu Developing ASP NET MVC web applications INTL (Trang 66 - 74)

Tải bản đầy đủ (PDF)

(390 trang)