Reputation: 449
I have an MVC project and in it I need to pass a variable between views. I'm trying to do this using a session variable but it stays null when I try to set it. What am I doing wrong? All of this code is in my controller.
public string searchQ
{
get { return (string)Session["searchQ"]; }
set { Session["searchQ"] = ""; }
}
public ActionResult Index()
{
Session["InitialLoad"] = "Yes";
return View();
}
[HttpPost]
public ActionResult Index(string heatSearch)
{
ViewBag.SearchKey = heatSearch;
searchQ= heatSearch;
return View();
}
public ActionResult Index_Perm()
{
ViewBag.SearchKey = searchQ;
return View();
}
The goal is to pass that search key between the views.
Upvotes: 1
Views: 774
Reputation: 148980
The problem is in your property setter.
public string searchQ
{
get { return (string)Session["searchQ"]; }
set { Session["searchQ"] = ""; }
}
This will always assign the session variable an empty string.
It should look like this:
public string searchQ
{
get { return (string)Session["searchQ"]; }
set { Session["searchQ"] = value; }
}
Upvotes: 3