11

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

3
  • @Soner Gönül How do you get the yellow background? Commented Dec 20, 2015 at 11:14
  • You can use "Blokequote" for that. See: stackoverflow.com/editing-help#simple-blockquotes Commented Dec 20, 2015 at 11:22
  • OK, Thanks! I will use that next time! Commented Dec 20, 2015 at 11:25

1 Answer 1

23

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.