1

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.

Danger_Fox
  • 449
  • 2
  • 11
  • 33

1 Answers1

3

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; }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Thanks for the help. One more thing, when I click the link to to load the page Index_Perm, searchQ is null again. – Danger_Fox Apr 18 '13 at 23:47
  • @Danger_Fox AFAIK, that should fix it, but this question offers a number of tips: http://stackoverflow.com/questions/10629882/asp-net-mvc-session-is-null – p.s.w.g Apr 18 '13 at 23:54