-3

I have an MVC Session which is null (doesn't exist) - I am checking this and want to return user to the "Error" view.

I am however getting a "NullReferenceException" error - have tried link below but to no avail:-

How to check if session value is null or session key does not exist in asp.net mvc - 5

Where am I going wrong?

Screenshots showing code with Error:-

Error1

enter image description here

Ram
  • 527
  • 1
  • 10
  • 26

1 Answers1

3

There's few places you're making a mistake...

Firstly..

userdata is null and you're accessing Id..

You want a check if userdata is not null before accessing Id

Then...

You should check if the Session variable is not null before calling .ToString() on it..

In fact a simple

Session["NominationsSearchViewModel"] == null

should be sufficient since you're checking if it's null anyway..

Adrian
  • 8,271
  • 2
  • 26
  • 43