0

This is my controller code:

private string testVal;

public ActionResult Index()
{
    testVal = "test";
    return View();
}

public ActionResult NextView()
{
    if (testVal == null)
        Debug.WriteLine("testVal is null");

    return View();
}

Is it possible to remeber values like testVal after changing page? It seems that when redirecting it resets values (testVal in NextVal is null).

Edit:

I try to save values to session but Session is null. I am using SignalR and when user is connected to page i use static event from hub to inform controller that user has connected - but inside method that runs on that event Session is unfortunetly null.

My controller code:

public ActionResult Index()
{
    LoadingHub.userConnected += new EventHandler<IdEventArgs>(UserConnected);

    return View();
}

private void UserConnected(object sender, IdEventArgs e)
{
    Debug.WriteLine("User Connected with Id: " + e.Id);

    if (Session == null)
        Debug.WriteLine("Session is null");
}

My signalr hub:

public class LoadingHub : Hub
{
    public static event EventHandler<IdEventArgs> userConnected;

    //Function informs server that user has connected
    public void Connected()
    {
        Debug.WriteLine("Hub Connected Method");
        var id = Context.ConnectionId;

        userConnected(this, new IdEventArgs(id));
    }
}
Jakub Wisniewski
  • 2,189
  • 4
  • 20
  • 34

4 Answers4

1

Every time that you make a request a new instance of the controller is created so using a private field you will not be able to retain the value of this variable.

The easiest way for you to retain it it is to use a session. (if you want to retain this value per user base)

for example in your code

public ActionResult Index()
{
    System.Web.HttpContext.Current.Session["testVal"] = "test";
    return View();
}

public ActionResult NextView()
{
    if (System.Web.HttpContext.Current.Session["testVal"] == null)
        Debug.WriteLine("testVal is null");

    return View();
}
  • Can i store some more complicated object in session variables? – Jakub Wisniewski Mar 23 '15 at 10:05
  • For sure. you can store any object on the session. But I would suggest to always store objects that are serializable. and remember everything that you store on the session stay in the memory of the server by default. – Gabriel Monteiro Nepomuceno Mar 23 '15 at 10:23
  • It is better for you to create a new question about using session on signalR the question that you have now it is completely diferent from the one that you started. it is better to keep your first question mark it as answered and than ask another. – Gabriel Monteiro Nepomuceno Mar 23 '15 at 20:55
1

you can use cookie or cache to replace the variable. when you redirect to a webpage ,the controller will be newed ,so you cannot get the right testVal .but the cookie is stored in broswer .so you can set it and get .

Fururur
  • 11
  • 2
1

Have you looked into ASP.NET server side state management click here.

These are basically different ways to remember a value on the server once a new page has been loaded.

So a few server side techniques you could use to remember testVal are Session State or Application State. However Session State is more suitable for your scenario as it is only specific to the user's current session whereas Application State stores data that can be shared between sessions and would therefore be more ideal for global variables.

You can read the link I provided to read more on the differences though.

I would also like to warn you (as some say to use cookies), the user can delete or disable or manipulate them on the browser so this isn't an ideal solution.

Will.Harris
  • 4,004
  • 2
  • 24
  • 37
  • 1
    Jakub Wisniewski - I can't comment under the other answer (because my points score is lower than 50 :/) but yes you can store complicated objects in the session. See this post http://stackoverflow.com/questions/11955094/asp-net-is-it-possible-to-keep-complex-object-such-as-list-is-the-session – Will.Harris Mar 23 '15 at 10:19
  • 1
    I've never used SignalR Hubs so I really couldn't advise you here. But from a quick Google search it looks as though it doesn't support Sessions (which is why its returning null for you) so I wouldnt know what alternative approaches there are, sorry. http://stackoverflow.com/questions/7854663/signalr-doesnt-use-session-on-server – Will.Harris Mar 23 '15 at 14:30
1

You may use session or Pass the data to the controller

Muhd Alavu
  • 94
  • 1
  • 7