Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get object from tempdata in MVC 4

I have employee class

public class Employee
{
    public string Name { get; set; }
    public string EmpID { get; set; }
    public string Designation { get; set; }
}

I want the same using TempData on View.... if I use @TempData["Employee"]

I am getting modal.employee which is fair enough..

please suggest

like image 360
NishantMittal Avatar asked Jan 11 '16 09:01

NishantMittal


Video Answer


1 Answers

To set data in TempData -

TempData["Employee"] = new Employee() {Designation = "Manager"};

To retrieve it in view -

@{
    var emp = TempData["Employee"] as Employee;
}

Use emp variable in later part of the view.

<div>@emp.Designation</div>

IMPORTANT Any object in TempData will be removed once it is read (or)retrieved. To keep it in TempData for further usage, use Tempdata.Keep()

TempData.Keep("Employee");

Alternatively you can use ViewBag to send data from Controller to View.

like image 150
ramiramilu Avatar answered Nov 15 '22 13:11

ramiramilu