Tomas
Tomas

Reputation: 18097

pass data between Actions

I have tried to pass data from one Action to another in the same Controller but it seems this do not work. I get null. How to pass value between controllers?

Action1

 ViewData["ProductId"] = productId;

Action2

var productId = ViewData["ProductId"];//Always Null

Upvotes: 0

Views: 2368

Answers (3)

Yorgo
Yorgo

Reputation: 2678

how did you call the second action inside first action? If you call

RedircectToAction("Acion2"). 

then you can pass the parameter by action property like this:

RedirectToAction("Action2", new { productID= ViewBag.ProductId});

public ActionResult Action2(Guid productID)
{
}

Upvotes: 0

Romias
Romias

Reputation: 14133

You can also use the TempData to share data until the next request.

Upvotes: 1

PinnyM
PinnyM

Reputation: 35533

You could use this.Session["ProductId"], but this approach is generally incorrect and leads to difficult-to-maintain code that isn't scalable. More likely, you should be passing this to your view to render as an input field (for a form), or for generating urls (for links).

Upvotes: 1

Related Questions