Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access session variable in View

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

like image 431
front-back Avatar asked Dec 20 '15 11:12

front-back


People also ask

Can a user view session variables?

No, SESSION variables are on the server side so from the client's perspective, they cannot change them.

How can use session value in MVC view?

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.

Which variable is used to access the session variables?

Session variables are set with the PHP global variable: $_SESSION.

Can I change session value in browser?

# 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.


1 Answers

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

like image 169
dotnetom Avatar answered Oct 12 '22 04:10

dotnetom