Monday, 11 June 2018

Difference Between ViewData, ViewBag and TempData?

ViewData
ViewData is used to pass data from controller to view.
It is derived from ViewDataDictionary class.
It is Key-Value Dictionary collection.
It is available for the current request only.
Requires typecasting for complex data type and checks for null values to avoid error.
If redirection occurs, then its value becomes null.
 Example
In the below example, a string value is set in the ViewData object in Controller and it is then displayed in View.
Controller
public class HomeController : Controller
{
// GET: /Home/
    public ActionResult Index()
    {
        ViewData["Message"] = "Hello LearningPoint92";
        return View();
    }
}

View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewData["Message"]
    </div>
</body>
</html>
    ViewBag
ViewBag is also used to pass data from the controller to  view.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
It is a type object.
It is also available for the current request only.
Doesn’t require typecasting for complex data type.
If redirection occurs, then its value becomes null.
    Example
In the below example, a string value is set in the ViewBag object in Controller and it is then displayed in View.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
ViewBag.Message = " Hello LearningPoint92";
return View();
}
}

View
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Message
</div>
</body>
</html>
  TempData
TempData is used to pass data from the current request to the next request.
TempData is derived from TempDataDictionary class.
It is Key-Value Dictionary collection.
TempData only works during the current and subsequent request.
It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action.
It requires typecasting for complex data type and checks for null values to avoid error.
Example
In the below example, a string value is set in the TempData object in index view and it is redirected to another view named as Display and finally it is displayed in View.
HomeController
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
        {
            TempData["Message"] = " Hello LearningPoint92";
            return RedirectToAction("Display");

      
        }

         public ActionResult  Display()
        {
          
            return View();


        }
}
 Display(view)
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@TempData["Message"];
</div>
</body>
</html>

RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace temp_app
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}


1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...