3

I have an MVC controller and I wish to pass the same (static) information to any ActionResult in the same controller, changeable only by a new choice in the index page by the same user. I have read that it is considered bad practice to use a static variable. My sites use Windows Authentication in an Intranet environment, and up to 10 people can be looking at any one page at any time. If I understand what I read correctly, there is a danger that the static variable can be overwritten by someone other than the page user, simply by viewing the same page at the same time.

As an alternative, I read about "TempData" and "Session Variables", but so far I have not read anything indicating whether these approaches would ensure that the variable is set in the Index page by only the person viewing that instance of the page. I have pasted code samples below that show a general idea of what I mean. I have gotten them to work, my question is which method ensures that only the person viewing that instance of the page sets and reads the value?

This code sample shows use of a controller-level static variable:

public class HomeController : Controller
{
    public static string _currentChoice;
    public ActionResult Index(string CurrentChoice)
    {
        _currentChoice = string.IsNullOrEmpty(CurrentChoice)?"nothing":CurrentChoice;
        ViewBag.Message = "Your choice is " + _currentChoice;
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your choice is still "+_currentChoice;
        return View();
    }
}

This code sample uses TempData and Session:

public class HomeController : Controller
{     
    public ActionResult Index(string CurrentChoice)
    {
        var _currentChoice = CurrentChoice;
        _currentChoice = string.IsNullOrEmpty(CurrentChoice)?"nothing":CurrentChoice;
        TempData["CurrentChoice"] = _currentChoice;
        Session["SessionChoice"] = _currentChoice;
        ViewBag.Message = "Your choice is " + _currentChoice;
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your choice is still " + TempData["CurrentChoice"]
            + "\nYour Session choice is " + Session["SessionChoice"]; 
        return View();
    }
}
  • If the architecture of your app is a server farm, you'll need to use session state or it might not get shared between requests. – Shahzad Qureshi Jul 21 '15 at 16:13
  • That's a good rule I can write down. But what if I know for a fact that there is only a single web server and not a server farm? –  Jul 21 '15 at 16:14
  • Using ViewBag Is the best place for sharing data between C and V – Alex Jul 21 '15 at 16:16

1 Answers1

11

You'll want to use the Session. Each of the options you presented have different use cases:

  • Static variables use the same variable for every instance of the class. Meaning every user will see the same value, and if one user changes the variable, it changes for all the others. Since you want it to be unique per user, this is not an option.
  • TempData is for passing data during redirects, according to this answer
  • Session data is for storing data for the current session, and will be unique per user.
Community
  • 1
  • 1
Will Eddins
  • 13,628
  • 5
  • 51
  • 85
  • 2
    Note that TempData is actually stored in session data, but each value is wiped out as soon as you access it. – howcheng Jul 21 '15 at 23:21