2

How to retrieve the session values which are stored in the list using javascript?

I used this code to set the session in the controller.

  List<test> _test= new List<test>();
  if (Session["testsession"] == null)                
            Session["testsession"] = _test;

I used this code to retrieve the session list values using javascript

  var TEST ='@HttpContext.Current.Session["quotesession"]';

but when i debug it the output is in

     var TEST ='System.Collections.Generic.List`1[NLG.IMS.Shared.Models.Test]';

Where i went wrong? I need the session values to be retrieved from the list.

1 Answer 1

3

I would advise using a strongly typed model and assigning a property to the collection rather than using session, even the ViewBag would be better but if you really must, this is how you could:

You could use the following:

var json = @Html.Raw(Json.Encode(@HttpContext.Current.Session["quotesession"]));

Which would output it as json.

jsFiddle

The above prints out a collection to the console.log.

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

5 Comments

Thank u. I also got the same result while using session also.
I need one more info. How to split those objects?
@AarthiRavendiran If you mean loop, I use jQuery.each: api.jquery.com/jquery.each
I dn't want to loop. i need to obtain specific values from that. What method should i need to use?
@AarthiRavendiran If it is a collection, you would access each property with either . notation or square brackets [] i.e. json[0].Property1 or json[0]["Property1"] would access the first one. Obviously use the property in your object though.

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.