0

I have two action results in my Controller. Overview and About.

public ActionResult Overview(int id)
{
    return View(new OverviewModel() { Project = db.People.Find(id) });
}

public ActionResult About(int id)
{
    return View(new AboutModel() { Project = db.People.Find(id) });
}

I would like to remember the id that was passed in to Overview and use it on the About as the default. I don't know how to keep this Id constant while the user switches tabs from Overview to About.

Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61

2 Answers2

5

You can try storing the id in TempData. Maybe something like this (not tested)

public ActionResult Overview(int id)
{
    TempData["YourId"] = id;
    return View(new OverviewModel() { Project = db.People.Find(id) });
}

public ActionResult About(int? id)
{
    id = id ?? int.Parse(TempData["YourId"].ToString());
    return View(new AboutModel() { Project = db.People.Find(id) });
}
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • Could you elaborate more on what TempData is? Thanks this does work. – Nate-Wilkins May 07 '12 at 20:18
  • @dnxviral, TempData is like ViewData, except that it persists for two successive requests making it useful for things like passing data between two different controller actions. – Brandon May 07 '12 at 20:27
  • 2
    TempData in MVC actually persists until retrieved. As an FYI Tempdata is actually stored in a users SessionState so it is more like SessionData than ViewData. http://msdn.microsoft.com/en-us/library/dd394711.aspx – Jason C May 07 '12 at 21:17
  • Thanks for the clarification! Is there a way to keep the data persistent, while retrieving it multiple times? – Nate-Wilkins May 07 '12 at 23:51
0

You can also use hidden html attribute if this data is not sensitive. it worked wonders for us and it saved us alot of processing power.

Luke101
  • 63,072
  • 85
  • 231
  • 359