asp.net mvc save user selection globally - c#

I am developing a mvc 5 web application which allow user to select language and currency to be displayed to them, in which the choice of language and currency they selected is expected to be retrievable throughout the controller and view.
Do note that the user i referring here is anonymous user (which is user that do not log in) so i do not intend to save their selection to database.
I am thinking of using session to store the selection. However, it seems not a good choice of to me as these are only two values that i need to store in session. Is there any other better alternative?

An alternative is to put language in url
{language}/{controller}/{action}/{id}
e.g.
en/home/index
fr/home/index
Advantage is that the url is bookmarkable and clearer
An example implementation: http://adamyan.blogspot.sg/2010/07/addition-to-aspnet-mvc-localization.html
For currency, I'll probably determine it from the culture of user, or at least use that value as default for first time user. If you want to let user be able to change it, cookie is fine in my opinion.

Related

Localization for user profile information Asp.net

I'm trying to implement page localization to my webpage made in asp.net-mvc. I understand how to implement titles and other modular data.
The problem comes when i want to give user/admin of this page rights to change his personal information/bio on her webpage if needed. Do i save the bio in the database? With another column in the table which indicates for which language it is meant?
Or is there any better solution?

Advice for State Management on an Individual Page

I've got a problem that may either be a limitation in my approach or asp.net web forms technology. The main driver for this solution is the simple fact that when users sit on an individual page for an extended period of time, they are redirected to our CAS server and back to the page itself which results in all of the form inputs being cleared as if the user did not enter any information (i.e. losing the ViewState/control state). With that being said I was exploring other options such as:
Using HTML localStorage to store a JSON string of all values on the page that can be parsed into an object and loaded into the form later if needed.
Even though this solution allowed me to store values on each page in individual key/value pairs (page instance 1 in localStorage[ID1], page instance 2 in localStorage[ID2] etc.), there was still a limitation on the server side of validating which page the user is saving. Right now I am using the session to maintain which form the user is viewing ie. the user is only associated with 1 page. To continue the example, the session information would result in the user being associated with either localStorage[ID1] or localStorage[ID2], not both ---which is precisely my problem. I need to communicate from the client-side which one page is to be saved. I've explored the following options, and come up with the following questions:
A static class that contains a static collection of objects on the server side storing which users have which pages open--> problem: communicating which page is actually being saved.
Dynamically rendering my save buttons with IDs that include an encrypted version of the page identifier at hand.
Storing the current value being edited in the Querystring
A cookie value that stores the value of the ID that the user wishes to change on post back-->how will the cookie get its value--especially when an ID is new (user is filling out form for the first time)?
I may be over complicating this and I know there is an easier way.
Many sites solve this problem by displaying a dialog when the user has sat on the page too long, asking if they still want to maintain their session. If the user doesn't click "Yes", then lose the session.
Another solution would be to permit a user who knows he's going to not use the page for a while to save the results so far and to restore them later, and continue.
Before proceeding with a technological solution, I'd try to find out why the users are sitting on the page so long. Whatever their reason is, you'll want to try to accommodate it if it's a good reason.
If the reason is "because we don't really care about your page and just didn't bother to close it", then your current solution works fine.
I just wanted to close out my own issue. I will resolve my problem by storing encrypted values in the query string to maintain page state. From there I will all values the user enters on the page within stringified json objects within localStorage. For example: page 1 with id: aa will have one key value pair within localStorage in the format [appName_pageId]. Page 2 with id bb will have a similar key value pair. If the user gets redirected to our CAS server and lands back on my page with all values cleared, I will be able to load the correct inputs from localStorage based on the value in the query string parameter (aa or bb). The important principle to remember here is that central control belongs on the server and distributed control should be done on the client.

How do you store custom user data in asp.net?

I may be overlooking a question like this, but I couldn't find it. I have an asp.net site with C# codebehind, and I have a working means of creating user accounts, passwords, and associating other data with each user. I started this site by creating a web application, and the login/register functions were all included and work fine. I also have a database that the user can search and display data on the site (all of this also works). However, I want the user to be able to search the database for data, and then be able to go back to pages that they've saved as pages that they're "following." All I would need to store would be a unique identifier from the database to note that the person wanted to follow that page. Where would be the best place to store these values? Could I do so by saving the data in the aspnetdb database? If so, how would I write custom values in this database if using an asp.net web application?
If you're using ASP.NET's Membership feature, you'll probably want to use the MembershipUser.ProviderKey as the unique ID mentioned by #dcreight. Use it to access your own table. I would not recommend writing into the aspnetdb directly.

User Session using different languages and MVVM

I am writing a desktop application using MVVM and Prism and I have the following problem. As well as the labels etc it is also necessary to store data in different languages.
For example, in the USA a user might be able to login using either English or Spanish (as they are a native speaker).
If the user logs in using Spanish when they go into a product details
form the product description will be saved to the database against
the Spanish language.
If they log in using English the product description will be saved to
the database and related to the English language.
At the moment my only ideas is as below:-
- a globally accessible ApplicationViewModel that contains a
CurrentLoggedInUser property
What I would like to know is how I go about saving the currently logged in user's chosen language when using MVVM, particularly in a desktop application?
N.B the chosen language can be different to the language and locale that the operating system is in
How do other people go about resolving this and maintaining the user's "session" across different windows / user controls when using MVVM?
All you need to do is store the current language somewhere, maybe as an LCID.
Generally this is used to load a language specific resource file, which takes care of most language dependant situations. Controls etc just store all their strings in resources, so they don't need direct access to the current language.
For situations where you do need access to the language, such as knowing where to save your database string, then yes, the language should be exposed either as some static/singleton, or more desirably, should be injected into whatever ViewModels need it.

How to persist a sorting option in a RadGrid

Hi I have a RadGrid in my asp.net application and basically I am being asked to persist the sorting clicked on the header name throught out the user session so basically lets say I have the RadGrid with the following headings.
ID Name OrderNumber Shipped
So by default it is sorted in ASC order by name when you first arrive at the page, but now.. if the User resorts it by clicking on the Name heading again it will make it in DESC order. So what I am being asked to do is to make the web remember the sorting option no matter if the user navigates to a different page within the application. So when the user comes back he should see the gridview in the order he/she picked before leaving to another page within the appliacation.
How can I do this with a RadGrid, without using HttpCookie ?
I do not think I should show my code as it is the plain default set up of a RadGrid BUT if necessary I will just grab something and put it here, I am just not sure the best way to go about it.
Thank you
I don't think this is a feature that we should be looking from a third party control, rather than an implementation item when using it in a project.
So if the requirement is only to persist within the user session, HttpCookie or even ASP.Net session would work. But if the requirement is to maintain even across same users next log-ins, even database would be a go od option rather than putting the weight on the ASP.Net session or Cookie transferring client to server every time.

Categories