Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between ViewBag, ViewData, or TempData in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 552
    Comment on it

    Difference between ViewBag, ViewData, or TempData in MVC:-

    In Asp.Net MVC you have three ways to pass data from controller to view and in the next request. They are ViewData, ViewBag and TempData.

    ViewData:- Viewdata helps to maintains data when you move controller to view. It is a is a dictionary object that is derived from ViewDataDictionary class.

    public ViewDataDictionary ViewData { get; set; } 
    

    It is used to pass data from controller to corresponding view. It is available for only current request. If redirection is done then its value is null. Viewdata requires typecasting and also need to check null values to avoid errors.

    public ActionResult Index()
    {
        ViewData["Name"] = "Rahul Joshi";
        return View();
    } 
    
    In view:----
     @ViewData["Name"] 
    

    ViewBag:-

    It's a dynamic wrapper around the viewData, it is used to pass data from controller to corresponding view. It's a dynamic property that takes advantage of the new dynamic features in C# 4.0. It life also lies in current request. If redirection done then ViewBag value is null. It does not required the typecasting.

    public Object ViewBag { get;} 
    
    public ActionResult Index()
    {
         ViewBag.TwingleImage="userprofile.jpg";
        return View();
    }
    
    In view:----
    @ViewBag.TwingleImage
    

    TempData:-

    It's helps to maintain data when you move from one controller to another controller and one action to another action. It internaly uses session variable. Basically tempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

    public TempDataDictionary TempData { get; set; } 
    
     //Controller Action1 (TempStudent)
    public ActionResult TempStudent()
    {
                    Student objectStudent = new  Student
                    {
                            StudentID = "MC09A35",
                            FirstName = "Rahul",
                            LastName = "Joshi"
                    };
                    TempData["StudentData"] = objectStudent;
                    return RedirectToAction("StudentDetail");
    }
    
     //Controller Action2(StudentDetail)
    public ActionResult StudentDetail()
    {
                   Student objectStudent = TempData["StudentData"] as Student;
                   return View(objectStudent);
     }
    

    Figure: Difference between Tempdata, Viewdata, and Viewbag alt text

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: