Hi, I am Facing an issue with session. I am creating a session like:
(
Game = Players;
Session["Game"] = Game;
)
But When I am Trying to use This Session in Another Action Mehtod Then I am Getting Null value . i have allready Rnd on this topic but I am find nothing in google
- if (Session["Game"] != null)
- {
- test = true;
- }
- PlayGame = (Players)Session["Game"];
Please give me a suggestion for That How to fix this issue
Here is My Code:-
1. > Code Where I am Creating Session
- public ActionResult EnterGame(Players Players)
- {
- if (!(string.IsNullOrEmpty(Players.FirstPlayer) && string.IsNullOrEmpty(Players.SecondPlayer)))
- {
- Players Game = new Players();
- Players.DefaultPlayer = Players.FirstPlayer;
- Game = Players;
- Session["Game"] = Game;
- bool test;
- if (Session["Game"] != null)
- {
- test = true;
- }
- return View("PlayGame", Game);
- }
- else
- {
- ModelState.AddModelError(string.Empty, "Both Players Name,s Are Required");
- return View("Index", Players);
- }
- }
###### Code Where I am use Session to initilize The Session Data
- public ActionResult PlayGame(int index) {
- Players PlayGame = new Players();
- bool test;
- if (Session["Game"] != null)
- {
- test = true;
- }
- PlayGame = (Players)Session["Game"];
- PlayGame Game = new PlayGame();
- Game.Play(index, PlayGame);
- if (Game.IsWon == true)
- {
- var Result = new { Message = Game.Message, IsWon = Game.IsWon, AssignedValue = Game.AssignedValue };
- return Json(Result, JsonRequestBehavior.AllowGet);
- }
- else
- {
- var Result = new { Message = Game.Message, DefaultPlayer = PlayGame.DefaultPlayer, AssignedValue = Game.AssignedValue };
- return Json(Result, JsonRequestBehavior.AllowGet);
- }
- }