Keith Nicholas
Keith Nicholas

Reputation: 44288

How to pass context around in a ASP.NET MVC web app

Ok, I'm a newbie to ASP.NET web apps... and web apps in general. I'm just doing a bit of a play app for an internal tool at work.

given this tutorial...

http://www.asp.net/learn/mvc-videos/video-395.aspx

The example basically has a global tasklist.

So if I wanted to do the same thing, but now I want to maintain tasks for projects. So I now select a project and I get the task list for that project. How do I keep the context of what project I have selected as I interact with the tasks? Do I encode it into the link somehow? or do you keep it in some kind of session data? or some other way?

Upvotes: 1

Views: 691

Answers (4)

Troels Thomsen
Troels Thomsen

Reputation: 11617

As it sounds like you are having multiple projects with a number of tasks each, it would be best practise to let the project be set in the URL. This would require a route such as "/projects/{project}/tasks". It follows the RESTful URL principle (i.e. the URL describes the content).

Using session state will not work if a user possibly have different projects open in multiple browser windows. Let's say I am logging into your system and a selecting two projects opening in two tabs. First the session is set to the project of the first opened tab, but as soon the second tab has loaded, the session will be overwritten to this project. If I then do anything in the first tab, it will be recorded for the second project.

Upvotes: 5

stucampbell
stucampbell

Reputation: 6603

I use:

  • Session state for state that should last for multiple requests, e.g. when using wizards. I'd be careful not to put too much data here though as it can lead to scalability problems.
  • TempData for scenarios where you only want the state to be available for the next request (e.g. when you are redirecting to another action and you want that action to have access to the state, but you don't want it to hang around after that)
  • Hidden form fields [input type="hidden"] for state that pertains to the form data and that I want the the controller to know about, but I don't want that data displayed. Also can be used to push state to the client so as not to overburden server resources.

Upvotes: 1

stephbu
stephbu

Reputation: 5082

RESTful URLs, hidden fields, and session cookies are your friends.

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44288

ok, From what I can tell, the best option seems to be to save it into the Session data

Upvotes: 0

Related Questions