front-back
front-back

Reputation: 277

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

Upvotes: 11

Views: 55886

Answers (1)

dotnetom
dotnetom

Reputation: 24901

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

Upvotes: 23

Related Questions