【发布时间】:2012-12-03 18:54:53
【问题描述】:
这是我在 ASP.NET MVC (MVC3) 中的第一个项目,我在处理所有事情时遇到了很多麻烦。根据选择的语言,我在页面中的所有文本都将从数据库中选择。对于这种语言选择,我更喜欢使用 Session 变量。我需要一个语言的图片链接,所以我将以下行写入 .cshtml 页面。
@using (Html.BeginForm()) {
<a href='<%: Url.Action("Index", "Home", new { lang="Tr" }) %>'>
<img src="../../Content/Images/flag_tr.jpg" width="40" height="20" />
</a>
<a href='<%: Url.Action("Index", "Home", new { lang = "En" }) %>'>
<img src="../../Content/Images/flag_en.jpg" width="40" height="20" />
</a>
}
在HomeController中:
public ActionResult Index()
ViewBag.Message = "Welcome to ASP.NET MVC!";
ViewBag.Selected = "Not yet selected";
return View();
}
[HttpPost]
public ActionResult Index(String lang)
{
if (lang == "Tr")
{
System.Web.HttpContext.Current.Session["Language"] = "Tr";
}
else if (lang == "En")
{
System.Web.HttpContext.Current.Session["Language"] = "En";
}
ViewBag.Selected = System.Web.HttpContext.Current.Session["Language"];
return View();
}
当我单击标志链接时,我收到“HTTP 错误 400 - 错误请求”。谁能告诉我我做错了什么,或者我应该怎么做?
P.S.:我也试过不使用 Form,在 Controller 中添加一个名为 Lang 的新函数并从那里重定向到 Index,但没有成功。
【问题讨论】:
标签: asp.net-mvc session session-variables