Session property

In controller , how/syntax?

Page 1 of 1

8 Replies - 3842 Views - Last Post: 21 November 2010 - 05:24 AM

#1 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Session property

Posted 17 November 2010 - 09:54 AM

Hi,

This might be more of a C# question but i am wondering about the Session property that you've access to in the controller of ASP.NET MVC2.
What i wanna do is essentially save my "SecretNumber" Class in a session, so i don't have to initilize a new class between posts.

Controller basic code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Labb1.Models;

namespace Labb1.Controllers
{
    public class homeController : Controller
    {
        //
        // GET: /home/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(SecretNumber model)
        {
//Session.Add(??) Or how do i utilize it.
            model.Initialize();
            model.Count = 6;
            if (ModelState.IsValid)
            {
                model.CheckGuess();
                return View("SecretNumber", model);
            }
            else
            {
                return View("Index");
            }

        }
    }
}



Thanks for any help/hints,
I've been trying to find info on the property or tutorials but they've mostly returned results about other session related stuff.
I just need some basic help on how to add my SecretNumber object to a session and then how to retrieve it.

Session.Add("filename",model) I think is the correct way to do it?
How do i retrieve it?

I solved it by using C# typecasting , silly me forgot it :(

This post has been edited by heady89: 17 November 2010 - 03:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Session property

#2 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Session property

Posted 18 November 2010 - 01:54 AM

Hi, I've another issue. Here is my Controller code.
My problem is that i want to save the session in between posts, if i retrive the session in my ActionResult Guess how would
one go about to make that particular session to be apart of the next post? If it makes sense i want to be able to use the saved
model in the session for the next post but when i have (SecretNumber model) it seems to create a new instance every time? And without
having it, it will conflict with my other Guess method telling me i already have one with the same parameters :S

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Labb1.Models;

namespace Labb1.Controllers
{
    public class homeController : Controller
    {
        //
        // GET: /home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Guess()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Guess(SecretNumber model)
        {
            if (Session["model"] == null)
            {
                model.Initialize();
                Session.Add("model", model);
            }
   
            model = (SecretNumber)Session["model"];
     
            
            //model.Count = 6;
            if (ModelState.IsValid)
            {
                model.CheckGuess();
                Session.Add("model", model);
                //var Retrive = (SecretNumber)Session["model"];
                return View("Guess", model);
            }
            else
            {
                return View("Guess");
            }
        }
    }
}


This post has been edited by heady89: 18 November 2010 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 agent1   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Re: Session property

Posted 18 November 2010 - 03:58 AM

why are you using the session? you dont need to, you can just pass your secretnumber along in the parameters..
and in your guess post you should say RedirectToAction("Guess",model) not return View().
if you explain what you are trying to accomplish with your code it would help...
Was This Post Helpful? 0
  • +
  • -

#4 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Session property

Posted 18 November 2010 - 04:32 AM

View Postagent1, on 18 November 2010 - 02:58 AM, said:

why are you using the session? you dont need to, you can just pass your secretnumber along in the parameters..
and in your guess post you should say RedirectToAction("Guess",model) not return View().
if you explain what you are trying to accomplish with your code it would help...


Sorry i am very new to it :(
Essentially i want to create an application where you guess the numbers, a secret number is initialized and then the user has 7 guesses(there are more things but i guess its not relevant such as validation which i already have, and things like displaying all the guesses his made within this guess game).

So what i am having troubble with is how to keep the SecretNumber Object between posts, since i need to keep it updated, say after 7 guesses he can't do any more so it needs to be the same object passed between the posts, otherwise i would just keep starting a new SecertNumber object everytime and everything would be back to 0 and a new secret number would be made every single post.

I hope it makes it a bit more clear what the application is about :/.
So essentially passing the same SecretNumber object between posts back and forth depending on the outcome.
The tip i got was to use the session property in the controller for the entire object, still i am not sure how to utilize it in a good way.

This post has been edited by heady89: 18 November 2010 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 agent1   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Re: Session property

Posted 18 November 2010 - 05:30 AM

ok then the session is a good idea, what you should do is create a constructor in your home controller and then put your secret number into the session in there:

public HomeController()
{
  if(Session["SecretNumber"] == null)
     {
         Session["SecretNumber"] == whatever your secret number is
     }
}




now you will have access to that secret number across all posts for the duration of the session(by default I think its about 30 mins not sure)

you could put other things in the session as well like how many times the person has guessed etc
I would do it similar to this, just making up the properties
                [HttpPost]
	        public ActionResult Guess(SecretNumber model)
	        {
                    
	            SessionModel = (SecretNumber)Session["SecretNumber"];
                    if(SessionModel.NoOfTimesGuessed == 7) //however many chances you want to give them
                       {
                           //they lose the game, game ends...
                        }
                    if(SessionModel.Number == model.Number)
                       {
                           //then they win the game....tell them
                       }
                       else
                      {
                         SessionModel.NoOfTimesGuessed ++;
                         Session["SecretNumber"] == SessionModel;
                         return RedirectToAction("Guess");
                       }

	          }


excuse the numbers, i cant remeber how to get rid of em

This post has been edited by agent1: 18 November 2010 - 05:56 AM

Was This Post Helpful? 1
  • +
  • -

#6 agent1   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Re: Session property

Posted 18 November 2010 - 06:24 AM

there were one or two errors with my code but cant seem to edit it now
Was This Post Helpful? 0
  • +
  • -

#7 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Session property

Posted 19 November 2010 - 05:17 AM

View Postagent1, on 18 November 2010 - 05:24 AM, said:

there were one or two errors with my code but cant seem to edit it now


Hi again,

Thanks for taking the time to help i am currently trying to apply some of your suggestions.
For the controller part however i am getting an error when i use this:
It says i am trying to reference to an object when i am only checking if the session is null or not :S

The error is caused on line 3, not line 5 or inside it.
        public homeController()
	    {
	        if(Session["SecretNumber"] == null)
	         {
	             Session["SecretNumber"] = new SecretNumber();
	         }
	    }




Edit/Update
I noticed i couldnt check the session so early as in the controller so i made a method for it instead.
I'll get back regarding the rest soon.

This post has been edited by heady89: 19 November 2010 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

#8 heady89   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Session property

Posted 19 November 2010 - 08:11 AM

Thanks in the meanwhile.

This post has been edited by heady89: 19 November 2010 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 agent1   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Re: Session property

Posted 21 November 2010 - 05:24 AM

it must be that the session has not been created when the home controller constructor is called, the constructor on your home controller will be the first bit of code that is run in your app, you will have to move the code that assigns to the session into one of your other action methods, possibly your index method...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1