[英]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>
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.