我有两个链接说例如。英语和西班牙语。当用户点击英文链接时,它应该在会话中将语言设置为“英语”。
以下是我的aspx代码
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function SetLanguageSpanish() {
document.getElementById("Home_Language").value = 'Spanish';
document.getElementById("frmHome").submit();
}
function SetLanguageEnglish() {
/*var obj = document.getElementById("LangEnglish");*/
// <% Session("Language") = "English"%>;
document.getElementById("Home_Language").value = 'English';
document.getElementById("frmHome").submit();
}
</script>
<div class="vmenu">
<form id="frmHome" action="Home" method="post"">
<a id="LangEnglish" href="/" onclick="SetLanguageEnglish();" >English</a>
<a id="LangSpanish" href="/" onclick="SetLanguageSpanish();"> Spanish</a>
<h3><%= Html.Label(Model.SubTitle) %></h3>
<ul class="sbe">
<li class="sbe"><a href="/test1"><%= Model.Menu1%></a></li>
<li class="sbe"><a href="/Test2"><%= Model.Menu2%></a></li>
<li class="sbe"><a href="/Test3"><%= Model.Menu3%></a></li>
<li class="sbe"><a href="/Test4"><%= Model.Menu4%></a></li>
<li class="sbe"><a href="/Test5"><%= Model.Menu5%></a></li>
</ul>
<input id="Home_PageName" name="PageName" type="hidden" value="" />
<input id="Home_Language" name="Language" type="hidden" value="" />
<input id="Home_PageTitle" name="PageTitle" type="hidden" value="" />
<input id="Home_SubTitle" name="SubTitle" type="hidden" value="" />
<input id="Home_Menu1" name="Menu1" type="hidden" value="" />
<input id="Home_Menu2" name="Menu2" type="hidden" value="" />
<input id="Home_Menu3" name="Menu3" type="hidden" value="" />
<input id="Home_Menu4" name="Menu4" type="hidden" value="" />
<input id="Home_Menu5" name="Menu5" type="hidden" value="" />
</form>
</div>
</asp:Content>
我的控制器类代码如下:
Public Class HomeController 继承System.Web.Mvc.Controller
' GET: /Home
<OutputCache(Duration:=1800, VaryByParam:="none")>
Function Index() As ActionResult
Try
Dim oHome As New Home.Home
Dim oHomeModel As New Home.HomeVM
If (HttpContext.Session("Language") Is Nothing) Then
HttpContext.Session("Language") = "English"
End If
oHomeModel.Language = HttpContext.Session("Language").ToString()
Return View("Index", GetCaption(oHomeModel))
Catch ex As Exception
Return Nothing
End Try
End Function
<HttpPost()>
<OutputCache(Duration:=1800)>
Function Index(ByVal oHomeModel As Home.HomeVM) As ActionResult
Try
If ((oHomeModel.Language IsNot Nothing) Or (oHomeModel.Language.ToString() <> "")) Then
HttpContext.Session("Language") = oHomeModel.Language.ToString()
End If
Return View("Index", GetCaption(oHomeModel))
Catch ex As Exception
Return Nothing
End Try
End Function
出于某种原因,当我点击我的视图页面上的任何链接时。 Controller类get被解雇了。因此,会话中的值不会刷新。任何人都可以建议为什么会这样吗?
以下是我的模特课。
命名空间主页 公共类HomeVM
'The following property are used as parameter
Public Property PageName As String
Public Property Language As String
'The following are the form caption peroperty
Public Property PageTitle As String
Public Property SubTitle As String
Public Property Menu1 As String
Public Property Menu2 As String
Public Property Menu3 As String
Public Property Menu4 As String
Public Property Menu5 As String
End Class
答案 0 :(得分:2)
在我看来,你正在混淆服务器端和客户端代码。
<% Session("Language") = "English"%>;
当您首次加载页面时,您在JavaScript中执行的上述行将被执行,而该页面不会打印任何内容。如果您查看源代码,除了分号;
之外,您应该只看到其他内容。
当您单击调用SetLanguageSpanish()
的链接时,该功能无需执行任何操作,页面将定向到“/”。看看页面来源。
修改强>
这是一些更新的代码,它将提交一个id为“language”的隐藏输入和一个由function参数指定的值。
<强> HTML 强>
<form id="frmHome" method="post">
<a href="#" onclick="SetLanguage('English');">English</a>
<a href="#" onclick="SetLanguage('Spanish');"> Spanish</a>
<input type="hidden" id="language" name="language" value="" />
</form>
<强>的JavaScript 强>
function SetLanguage(language) {
document.getElementById("language").value = language;
document.getElementById("frmHome").submit();
}
VB.NET 我不是VB人,所以请仔细检查这里的语法
<HttpPost()>
<OutputCache(Duration:=1800)>
Function Index(ByVal language As string) As ActionResult
HttpContext.Session("Language") = language
'Do stuff
Return this.View()
End Function