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. There's probably some way to do it with select tags and option tags and some javascript but I'm just not sure how. Does anyone know how to do this?
我需要帮助。我正在尝试创建一个带有法语和英语文本的下拉框,而不是我现在在视图中的两个链接。我在MVC工作,我需要一个下拉框,根据用户的语言选择(没有提交按钮的额外步骤)自动提交,但只需根据选择发布。选择标签和选项标签以及一些javascript可能有一些方法可以做到,但我只是不确定如何。有谁知道如何做到这一点?
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 个解决方案
#1
0
- 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
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
<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)
#1
0
- 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
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
<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)