我想更改网页的语言,而不必向服务器发出请求以保持我网站的“状态”(表单,聊天,...) 以我改变语言的简单页面为例,但我不想丢失表单中的内容。
File.cshtml
@model MVCIdiomas.Models.Persona
@{
ViewBag.Title = Resource.Resource.AddPerson;
var culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name.ToLowerInvariant();
}
@helper selected(string c, string culture)
{
if (c == culture)
{
@:checked="checked"
}
}
<h2>@Resource.Resource.AddPerson</h2>
@using (Html.BeginForm("SetCulture", "Home"))
{
<fieldset>
<legend>@Resource.Resource.ChooseYourLanguage</legend>
<div class="control-group">
<div class="controls">
<label for="en-us">
<input name="culture" id="en-us" value="en-us" type="radio" @selected("en-us", culture) /> English
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label for="es">
<input name="culture" id="es" value="es" type="radio" @selected("es", culture) /> Español
</label>
</div>
</div>
</fieldset>
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="@Resource.Resource.Create" class="btn btn-default" />
</div>
</div>
</div>
}
Controller.cs
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
.
.
.
public ActionResult SetCulture(string culture)
{
// Validate input
culture = CultureHelper.GetImplementedCulture(culture);
// Save culture in a cookie
HttpCookie cookie = Request.Cookies["_culture"];
if (cookie != null)
cookie.Value = culture; // update cookie value
else
{
cookie = new HttpCookie("_culture");
cookie.Value = culture;
cookie.Expires = DateTime.Now.AddYears(1);
}
Response.Cookies.Add(cookie);
return RedirectToAction("Index");
}
}
如果没有提交到服务器,是否有任何表格可以进行更改?