简体   繁体   English

我需要将多语言链接更改为一个下拉框吗? 我怎样才能做到这一点

[英]I need to change the multi lingual links to a drop-down box? How can I do this

I need help. 我需要帮助。 I'm trying to create a drop-down box with the text French and English as opposed to the two links that I have now in my view. 我试图用文本法文和英文创建一个下拉框,而不是我现在看到的两个链接。 I'm working in MVC and I need to have a drop-down box that should auto-submit based upon the user's language selection (not having the extra step of a submit button), but just post based upon selection. 我正在MVC中工作,我需要有一个下拉框,该框应根据用户的语言选择自动提交(不具有“提交”按钮的额外步骤),而仅根据选择进行发布。 There's probably some way to do it with select tags and option tags and some javascript but I'm just not sure how. 可能有一些方法可以使用选择标签和选项标签以及一些javascript,但是我不确定如何。 Does anyone know how to do this? 有谁知道如何做到这一点?

Here is my controller 这是我的控制器

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");
    }
}

} }

Here is my View 这是我的看法

@{
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>
  1. In Controller 在控制器中
public ActionResult Change(string languageAbbrevation)
        {
            if (languageAbbrevation != null)
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageAbbrevation);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageAbbrevation);

            }

            HttpCookie cookie = new HttpCookie("Language");
            cookie.Value = languageAbbrevation;
            Response.Cookies.Add(cookie);
            Response.Redirect("Index");
            return View("Index");
        }

2.In Global 2,全球

protected void Application_BeginRequest(object sender, 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);
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pt");
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("pt");
            }
        }

3.In html 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.create resource files HomeTexts.en.resx HomeTexts.resx -pt default 4.创建资源文件HomeTexts.en.resx HomeTexts.resx -pt默认

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

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

相关问题 如何在手机上隐藏下拉菜单的子链接? - How can I hide sub-links of drop-down menu on mobile? 从SuiteScript的下拉菜单中选择选项后,如何显示文本框? - How do I make a text box appear after selecting an option from a drop-down menu in SuiteScript? 如何从AngularJS的下拉框中设置选择默认值? - How can I set a select default value from a drop-down box in AngularJS? 如何将一些结果附加到jQuery自动完成下拉框? - How can I append some results to a jQuery autocomplete drop-down box? 我正在尝试为小屏幕提供一个响应式导航栏,如何更改指向CSS下拉菜单的左侧链接? - I'm am trying to have a responsive navigation bar for small screens, how can I change the left links to a css drop-down menu? 如何使用html和js添加下拉菜单更改sci图表的主题? - How can I add a drop-down menu to change the theme of sci chart using html and js? 如何使div进入Bootstrap下拉菜单 - How do I make a div to a Bootstrap drop-down 如何按字母顺序对下拉菜单项排序? - How can I sort drop-down menu items alphabetically? 如何使用bootstrap创建下拉菜单? - How can I create a drop-down menu using bootstrap? 如何将下拉的值放入公式中? - How can I put the value of drop-down in formula?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM