简体   繁体   中英

Persisting DropDownList selection for return visits in MVC5

I've read dozens of articles and solutions on persisting a selected value of a DropDownList. None of the articles seem to address my need exactly. Here is my requirement:

I have a DropDownList in _Layout.cshtml (sitewide page header) that allows a user to select their desired language.

<form>
    <select id="lang">
        <option value="en-US" selected="selected">English</option>
        <option value="fr-CA">Fran&#231;ais</option>
        <option value="pt-BR">Portugu&#234;s</option>
        <option value="es-MX">Espa&#241;ol</option>
    </select>
</form>

My intention is to use the Westwind.Globalization package (NuGet) to perform localization.

By default the setting should be English, but when a user makes a selection from the language dropdown I want to persist their selection to a cookie which will be used for not only for the current session but also on return visits.

If the user is authenticated I will also want to save their selection in their user profile which I have already added a column for.

What is the best practice for persisting a value for the current session, return visits, and to save to the user's profile if they are authenticated when the desired language is changed?

If users of your website are anonymous, you can use a cookie to store the language preference.

If users of your website are not anonymous, in addition to the cookie you can store the language preferences of a user along with that user's database record.

The reason I say " in addition to the cookie " is the following. Suppose a user already has an account on your website and uses it regularly. They visit your website and have not logged in yet. The language preference should be drawn from the cookie until the user is logged in so that the landing page, the log in page, etc. are in the user's language. Once a user has logged in, update the cookie with the user's language preferences retrieved from your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM