Since I can't access a session variable in the View, I wonder if I need do add something more to the view to get it to work?
Inside my View:
@Session[ComputerNumber].ToString()
Controller:
Session["ComputerNumber"] = game.RandomNumber();
The error message:
Compiler Error Message: CS0103: The name 'ComputerNumber' does not exist in the current context
No, SESSION variables are on the server side so from the client's perspective, they cannot change them.
The Session value will be displayed using Razor Syntax in ASP.Net MVC. In the below example, a string value is set in the Session object in Controller and it is then displayed in View using Razor Syntax.
Session variables are set with the PHP global variable: $_SESSION.
# Edit sessionStorage keys or values View a domain's sessionStorage key-value pairs. Double-click a cell in the Key or Value column to edit that key or value.
You can use Session
in the view, you just need to use string indexer, just like in your controller. In your case ComputerNumber
is not a string, it is a variable which does not exist. Change
@Session[ComputerNumber].ToString()
to
@Session["ComputerNumber"].ToString()
and it should all be working
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With