Danger_Fox
Danger_Fox

Reputation: 449

Session variable remaining null

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

Answers (1)

p.s.w.g
p.s.w.g

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

Related Questions