3

I have tried to pass data from one Action to another in the same Controller.But always it is coming as null

Action1

ViewData["page"] = pageData;

Action2

var pageData = ViewData["page"];//Always Null

I have following requirement.

  1. Hit page and call Action1
  2. For same browser session store pageData in ViewData
  3. There will be one button on the page which will call Action2
  4. use ViewData["page"] to get pageData value which we have assign in Action1

Could anyone please help me to achieve this ?

Thanks

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
cbrebel
  • 85
  • 1
  • 9

4 Answers4

2

Try TempData:

TempData["page"] = pageData;

ViewBag and ViewData are used to pass data from controller to view.

TempData is used to pass data from the current request to the next request.

For additional info on difference you can refer to this link Difference Between ViewData, ViewBag and TempData.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • I have tried TempData["page"] = pageData as well but not working for me. have kept TempData.Keep() as well. but no luck – cbrebel Nov 08 '17 at 11:00
  • I tried to replicate it on my machine and it is working fine the same with your posted scenario. Are you sure that the next request was the Action method Action2? If not, TempData will be automatically null. Since TempData works only on "concurrent" request. – Willy David Jr Nov 08 '17 at 11:26
0

ViewBag or ViewData only lives from one controller action to the View.

To access the value in the next immediate action, try using TempData with the similar syntax as ViewData

Abdul Ali
  • 1,905
  • 8
  • 28
  • 50
0

What you going to do is To fill the ViewData["page"] Within the your action then pass it to View. since it only lives within your Controller [Action] -> View. then Store it within an element (It can be a Hidden Input) like:

<input type="hidden" name="ID" value=@ViewData["page"] id="ID" />

Then If you wish to receive it Within your Next ActionResult you can easily Call the FormCollection like below and take the Value from the Hidden Input:

public ActionResult Example(FormCollection form)
{
    var id = form["ID"];
}

Thats how it works with ViewData and ViewBag. However; If you want to Directly use the data within another action I recommend using TempData like below:

TempData["page"] = pageData;

There are number of other ways to receive the data within your other action as well such as Cookies, And Sessions but Unless you do not want to go to many trouble i recommend TempData.

UPDATE

Use Session, Next easiest option would be Session It works like below:

Create:

Session["page"] = YourValue;

Read:

//You can easy cast it to your type or Convert it.
var _strValue = Sessions["page"].ToString() //if its string 
Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44
0

try this

TempData["page"] = pageData;

it works with multiple actions but only one request.

Session["page"] = pageData;

works with multiple requests but it more heavy.

i hope this help you.

Lior
  • 508
  • 3
  • 18