0

My model class

public class User
{
    public string UserName { get; set; }      
    public string Password { get; set; }
    public List<string> Clients { get; set; }
}

I've put my model class in session

Session[Constants.User] = user;

In my view I want to use my Session[Constants.User], take out the list of clients from the model object and display.. So far I've got object in view containing everything using this,

var clientsList = (Session[Constants.User]);

How to take out list from that and display? Thanks in advance...

Uzair Khan
  • 2,812
  • 7
  • 30
  • 48
  • 1
    using Session is not the best way of going about doing things anyway. you should pass the data you want to display through your model and then bind it to the view, you also get it strongly typed in this instance which would be a win. – Ahmed ilyas Apr 27 '15 at 18:06
  • How are you looking to display this? divs, table, drop down, anchor tags...? – JasonWilczak Apr 27 '15 at 18:06
  • @Jason- How to separate the list from that session? – Uzair Khan Apr 27 '15 at 18:13
  • I'm not sure what you are asking? You are doing: var clientsList = (Session[Constants.user]); in your view (.cshmtl), right? You may have to parse as a List() – JasonWilczak Apr 27 '15 at 18:20
  • I added an answer below and added the bit about casting your session data to the correct type. – JasonWilczak Apr 27 '15 at 18:24
  • In a lower comment you mentioned that Constants.User is an object with one list property. I updated to reflect that. – JasonWilczak Apr 27 '15 at 18:52

2 Answers2

0

Ok, I agree that you should be using a Model to pass your user information, but, that being said, the simplest way is simple iteration. You will need to cast your variable first though (see this SO post about using Session).

@{
    var clientsList = (Session[Constants.User] as User).Clients;
}

@foreach(var user in clientsList)
{
    <div class="color_text"><h2>@user</h2></div>
}

You can construct your static html and then use a foreach around it to repeat for different user object data.

Community
  • 1
  • 1
JasonWilczak
  • 2,303
  • 2
  • 21
  • 37
0

If you had model:

public class User
{
    public string UserName { get; set;}
    public string Password { get; set;}
    public List<string> Clients { get; set;}
}

and you had

Session[Constants.User] = new User( { UserName = "someusername", Password =     "somepassword", Clients = new List<string>()  });

You can grab your user object from session like this:

User user = (User)(Session[Constants.User]);'

Then, you can loop through and display like this, assuming you are in view:

@foreach (String client in user.Clients)
{
    <div class="color_text">
        <h2>@client</h2>
    </div>
}
  • Sorry in place of "var clientsList" it should be "var user"..And it will get me User model because I've put my model in Session :-Session[Constants.User] = user; and this model has one list property..var clientsList = (List)(Session[Constants.User]); will give me model, how to take out Clients (which is list) out of this var clientsList? – Uzair Khan Apr 27 '15 at 18:47
  • In that case, you can take out Clients by doing: foreach (String client in user.Clients) – Lucko_MasterMind Apr 27 '15 at 19:00