DanilGholtsman
DanilGholtsman

Reputation: 2374

Problems with using Sessions in MVC

I am developing web application, nothing special.

There a lot of users and each user has got a lot of projects.

To store data I used the Session container.

When user was opening the project it was like (as example)

Session["projectId"] = current_project_id;
Session["projectName"] = current_project_name;

so as you see when user opening lets say 2nd project in other tab, data about 1st project just replaces with the 2nd project data.

It's really bad, yeah.

So I was interesting in how to build application in such way that different users could open their different projects in different tabs and everything was working fine, without any data loss.

I read about TempData and ViewBag, but still can't imagine how could I replace those Session variables.

What can help me? What should I use to store new data for each new tab?

Upvotes: 0

Views: 325

Answers (1)

Brendan Green
Brendan Green

Reputation: 11924

You could store a List or Dictionary of projects in the Session:

var projects = new Dictionary<int, string>();
projects.Add(projectId, projectName);

Session["UserProjects"] = projects;

And later:

var projects = (Dictionary<int, string>)Session["UserProjects"];

Upvotes: 2

Related Questions