简体   繁体   English

在MVC5中持续使用DropDownList选择进行回访

[英]Persisting DropDownList selection for return visits in MVC5

I've read dozens of articles and solutions on persisting a selected value of a DropDownList. 我已经阅读了数十篇有关持久保存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. 我在_Layout.cshtml(站点范围页眉)中有一个DropDownList,它允许用户选择所需的语言。

<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. 我的意图是使用Westwind.Globalization包(NuGet)进行本地化。

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. 默认情况下,设置应为英语,但是当用户从语言下拉列表中进行选择时,我希望将其选择保留到cookie中,该cookie不仅用于当前会话,还用于回访。

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. 如果您的网站用户是匿名用户,则可以使用cookie来存储语言首选项。

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. 如果您的网站的用户不是匿名用户,则除了Cookie之外,您还可以存储该用户的语言偏好以及该用户的数据库记录。

The reason I say " in addition to the cookie " is the following. 我说“ 除了cookie之外 ”的原因如下。 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. 在从用户登录之前,应该从cookie中提取语言首选项,以便登录页面,登录页面等使用用户的语言。 Once a user has logged in, update the cookie with the user's language preferences retrieved from your database. 用户登录后,请使用从数据库中检索到的用户的语言首选项来更新cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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