0

我需要帮助。我正在尝试创建一个带有法语和英语文本的下拉框,而不是我现在认为的两个链接。我在 MVC 中工作,我需要一个下拉框,它应该根据用户的语言选择自动提交(没有提交按钮的额外步骤),但只是根据选择发布。可能有一些方法可以使用选择标签和选项标签以及一些 javascript,但我只是不确定如何。有谁知道如何做到这一点?

这是我的控制器

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

 namespace MultiLanguage.Controllers
{
public class LanguageController : Controller
{
    // GET: Language
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Change(String LanguageAbbrevation)
    {
        if(LanguageAbbrevation !=null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation);
        }
        HttpCookie cookie = new HttpCookie("Language");
        cookie.Value = LanguageAbbrevation;
        Response.Cookies.Add(cookie);

        return View("Index");
    }
}

}

这是我的看法

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<ul>
<li>@Html.ActionLink("English","Change","Language",new {LanguageAbbrevation = "en"}, null)</li>
<li>@Html.ActionLink("French", "Change", "Language", new { LanguageAbbrevation = "fr" }, null)</li>
<li>@DateTime.Now.ToString()</li>
</ul>
4

1 回答 1

0
  1. 在控制器中
公共 ActionResult 更改(字符串语言缩写)
        {
            如果(语言缩写!= null)
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageAbbrevation);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageAbbrevation);

            }

            HttpCookie cookie = new HttpCookie("语言");
            cookie.Value = 语言缩写;
            Response.Cookies.Add(cookie);
            Response.Redirect("索引");
            返回视图(“索引”);
        }

2.在全球

        protected void Application_BeginRequest(对象发送者,EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
            }
            别的
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pt");
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("pt");
            }
        }

3.在html中

                    <div class="dropdown pull-right">
                        <button class="btn btn-link dropdown-toggle" type="button" data-toggle="dropdown">
                            @Html.Label(Project.Resources.HomeTexts.Language)
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            <li>@Html.ActionLink("English", "Change", new { languageAbbrevation = "en" }, null)</li>
                            <li>@Html.ActionLink("Portugues", "Change", new { languageAbbrevation = "pt" }, null)</li>
                        </ul>
                    </div>

4.创建资源文件 HomeTexts.en.resx HomeTexts.resx -pt default

5.然后使用标签@Html.Label(Project.Resources.HomeTexts.Login)

于 2016-09-17T20:02:41.443 回答