Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • TempData in asp.net mvc.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 169
    Comment on it

    TempData in asp.net mvc

    • TempData is a dictionary object derived from TempDataDictionary.
    • TempData provide communication between Controller action and it's corresponding view by tranfering and maintaing data.
    • The tempdata is retained not only for current request but also retained in case of redirection. It will live till redirected view is fully loaded. 

    Example demonstrating the use of TempData:

    EmployeeController actions

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Firstmvcapplication.Models;
    namespace Firstmvcapplication.Controllers
    {
        public class EmployeeController : Controller
        {
    
            public ActionResult tempemployee()
            {
                Employee emp = new Employee
                {
                    Empid = 1,
                    Empname = "suraj"
                };
                TempData["employee"] = emp;
                return RedirectToAction("permanentemployee");
            }
            public ActionResult permanentemployee()
            {
                int a = 0;
                Employee employee = TempData["employee"] as Employee;
                return View(employee);
            }
    
        }
    }


    EmployeeModels

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Firstmvcapplication.Models
    {
        public class Employee
        {
            public String Empname { get; set; }
            public int Empid { get; set; }
        }
    }

    permanentemployee.cshtml

    @model Firstmvcapplication.Models.Employee
    @{
        ViewBag.Title = "permanentemployee";
    }
    
    <div>
         @Html.DisplayNameFor(Model => Model.Empid)
         @Html.DisplayFor(Model => Model.Empid)
    </div>
    <div>
         @Html.DisplayNameFor(Model => Model.Empname)
         @Html.DisplayFor(Model => Model.Empname)
    </div>

    Output:

    Empid 1

    Empname suraj

    Explanation:

    In controller action two action tempemployee() and permanentemployee() is created. In action tempemployee() tempdata for employee is created and this action redirect to another action named permanentemployee(). In permanentemployee() tempdata from tempemployee() is converted and assigned to a variable employee,this variable is passed to corresponding view. In permanentemployee view data of tempdata is displayed. Thus tempdata retains value till redirected view is fully loaded.   
     

 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: