3

I have a Layout page which sets up the list of Projects in the application using Telerik's ComboBox as shown. The combobox allows user to select a project he/she wants to work on.

sample application

Once a selection is made, I want all subsequent actions in the application should correspond to the selected Project. I can preserve the Project information in a Session but then if user chooses to open this in a new tab and in 2nd tab users switches to a different Project and comes back to the first tab and refreshes the page then the session information (Project) would have changed which will create issues in my application.

So, what is the best way for me to persist Project information of the Layout.cshtml controls so that I can use it in my application such that every page that is rendered uses the currently/correctly selected values.

Tempdata / QueryStrings came to my mind but i don't know whether they will be reasonable solution to my problem. If yes, then how should I use them generically (specially querystrings) without complicating my solution?

localStroage and sessionStorage also seems like relevant solutions but then how do I use them in scenario where user opens a new tab from existing page? How will the Project # will persist on the newly opened page/window/tab?

WAQ
  • 2,556
  • 6
  • 45
  • 86

2 Answers2

3

something like this is achievable, if you make sure the url changes when a selection is made.

So let's say you select project C-1379 in your dropdown box, at that point your url could become http://localhost:58692/pid=C-1379.

from now onwards, your page can load the desired data, retrieving its required information from the query string. Do not use session or localstorage or anything like that as it won't work.

This way, you can still load your list of projects in your layout page, and you can select one based on the query string value and then load some default values via api calls to the back end.

If all your work from now on is done based on api calls, for example, you have some properties that you change and then you issue a POST to update said details then this is very easily done as well.

telerik controls usually have some events associated with them. the one you are using should have an onChange or something like that. This where where you would update the query string with the value of the project selected and then you can proceed to do what you need

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • so how do I ensure that my url always contains the pid=C-1379? Is there any default or automatic way of doing that? or manually in each request, I have to send this parameter? – WAQ Jan 23 '19 at 09:36
  • added a bit more information, look at the documentation of the specific telerik control and find the onChange or whatever event is triggered when the selection is changed. – Andrei Dragotoniu Jan 23 '19 at 10:10
  • but in my application there are going to be a lot of places where user will perform `Get` and `Post` requests, open links in new `Tab/Windows` etc. in all such cases I need to append the Project's information in the `Query String`. So, in order to avoid this extra effort (which is a lot, in terms of development and maintenance overhead), is there any simple solution through which the selected Project # always gets appended to the `Query String`? – WAQ Jan 23 '19 at 10:16
  • I think you need to add more information to what your issue is. Get and Post requests will run from a project page ( right? ) in which case you use javascript to populate it from the query string of the loaded project page. Opening new links in new tabs is done by clicking on links, so wherever you have a link you would make sure you get project id added, again with javascript. I don't see where the difficulty is. – Andrei Dragotoniu Jan 23 '19 at 10:21
  • like I said in original question that `_Layout` file will have a control populated with all Projects. Whatever Project is selected, every action throughout the application needs to correspond to this Project#. Most obvious solution is that any request (from any page/view/controller) should include the Project# parameter. I want to know whether their is an out of box solution to do this? so that I don't have to send Project # manually? e.g. `Session` variable was an option, but doesn't work for me because I want user's to work with different Projects in different `Tabs/Windows`. – WAQ Jan 24 '19 at 04:37
  • @WAQ I don't think you'll find a solution to your problem that doesn't require at least as much reworking as the solution presented here by Andrei. We have used MVC Routing to achieve a similar requirement in the past, but you still need to pass your ProjectId around in the appropriate places. The reason there's not a more simple out of the box way is because as you have realised, sessions and other things are shared between tabs. What you're trying to do achieve can't be managed by sessions and will have to be done with parameters. – Matt Shepherd Jan 25 '19 at 01:55
1

I can preserve the Project information in a Session but then if user chooses to open this in a new tab and in 2nd tab users switches to a different Project and comes back to the first tab and refreshes the page then the session information (Project) would have changed which will create issues in my application.

I would have thought this is the desired behavior... take stackoverflow.com as an example, if I change my username in one browser-tab, I would expect my username to be updated in other browser-tabs as well... and that's what happens if I refresh my other tab.

There is no built in solution for maintaining user info in different browser tabs separately... the only way to achieve this, is by sending project name back and forth in the URL... but then you would loose this info if user changes the URL... In my opinion, this is an ad hoc solution and does not worth the effort of development, because it's a very uncommon scenario.


Getting to your options:

  • Storing user info is a very typical use case for session variable.

  • TempData is stored in Session by default. Though you can write your own custom TempDataProvider and store it somewhere else (e.g. database, cookie, etc). See Brok Allen's Cookie TempDataProvider as an example. One advantage of using Cookie is that you send your session variable back and forth with the request so you don't need to worry about Sticky Sessions.

  • You can of course use a permanent storage, such as DB/Disk.

  • If the project name is not a sensitive info then I don't see any issue in passing it in Query String.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • All the features of the application will display data depending on the Project that user has selected in the `combobox`. In each tab/window, I want the user to be able to work with separate projects. This is very different from your example (username). – WAQ Jan 23 '19 at 07:34