Abdul
Abdul

Reputation: 1476

How to get the Count or Key of TempData in another ActionResult

I am setting the value of TempData in one ActionResult of different controller and trying to get their Keys of just counts in another controller's ActionResult

public ActionResult DealProducts(FormCollection form)
{
   TempData["check"] = "DealUpdated";
}

in another controller

public ActionResult CustomizedBudget()
{
   var temp = TempData["doc"];
   var temp = TempData["doc"].Key;//like this
   if (temp.Count > 0) // or trying to get like this, but not  
}

Upvotes: 0

Views: 2159

Answers (3)

Sambath Kumar S
Sambath Kumar S

Reputation: 407

To Assign

public ActionResult DealProducts(FormCollection form)
{
   TempData["check"] = "DealUpdated";
}

In CSHTML

@{
    TempData.Keep("check");
}

in another controller

public ActionResult CustomizedBudget()
{
   var count = TempData.Keys.Count;
   var DealUpdatedValue = TempData["check"];
}

Upvotes: 2

LateshtClick.com
LateshtClick.com

Reputation: 616

you need to change in below Action

public ActionResult CustomizedBudget() 
{ var temp = TempData["check"]; }

Upvotes: 1

Lidaranis
Lidaranis

Reputation: 785

well since you have

public ActionResult DealProducts(FormCollection form)
{
   TempData["check"] = "DealUpdated";
}

shouldn't you have

public ActionResult CustomizedBudget()
{
   var temp = TempData["check"];
   var temp = TempData["check"].Key;//like this
   if (temp.Count > 0) // or trying to get like this, but not  
}

?

Upvotes: 1

Related Questions