asp.net-mvc 将会话变量检索到 ASP.NET MVC 4 (razor, view)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17004616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:55:29  来源:igfitidea点击:

Retrieve Session variables into ASP.NET MVC 4 (razor, view)

asp.net-mvcasp.net-mvc-4session-variables

提问by michaldck

I wrote many websites with PHP. Now, I have to create website with ASP MVC 4 (c#) and I am stuck with Sessions.

我用 PHP 写了很多网站。现在,我必须使用 ASP MVC 4 (c#) 创建网站,但我被 Sessions 困住了。

I.E. the user should go to login page, enter his/her login and password. If they are correct, in controller, I set the session with UserId, like this:

IE 用户应该进入登录页面,输入他/她的登录名和密码。如果它们是正确的,在控制器中,我使用 UserId 设置会话,如下所示:

Session["UserId"] = 10

This UserId value is used for showing PartialViews (login form or (after login) some application menus). How can I get this UserId inside Razor view ?

此 UserId 值用于显示 PartialViews(登录表单或(登录后)某些应用程序菜单)。如何在 Razor 视图中获取此 UserId?

After this in View:

在此之后在视图中:

if (Session.UserId == 10) { @Html.Partial("LoggedMenu") }

i've got exception with StackOverflow. :/

我有 StackOverflow 的例外。:/

回答by balexandre

you're doing it wrong...

你这样做是错的...

Session[<item name>]returns a string, you should compare with a string as well, or cast it, so, either (int)Session["UserId"] == 10or Session["UserId"] = "10".

Session[<item name>]返回一个字符串,你应该有一个字符串进行比较,同时,或将其丢,所以,无论是(int)Session["UserId"] == 10Session["UserId"] = "10"

you also are invoking a property that does not exist Session.UserIdwill not exist as Sessionis like an NameValueCollection, you call it by request it's item name.

您还调用了一个不存在的属性,就像 NameValueCollection 一样Session.UserId不存在Session,您可以通过请求它的项目名称来调用它。

at the end, you should write

最后,你应该写

@if (Session["UserId"] == "10") { 
    Html.Partial("LoggedMenu"); 
}

You say your are learning, so I would like to point out 2 quick things:

你说你正在学习,所以我想指出两个快速的事情:

  • You should take advantage of the ASP.NET MVC Course that is available for free in the home page http://asp.net/mvc(right side when you read "Essential Videos")
  • Create an MVC3 project and see how they do it as it comes ready out of the box with Membership
  • 您应该利用主页http://asp.net/mvc 中免费提供的 ASP.NET MVC 课程(阅读“基本视频”时的右侧)
  • 创建一个 MVC3 项目,看看他们是如何做的

回答by Mathias F

@if (Session["UserId"] != null && Session["UserId"] == 10 ) { 
Html.Partial("LoggedMenu"); 
}

Apart from that: for identity management better use the out of the box membership system

除此之外:为了身份管理,最好使用开箱即用的会员系统

回答by Flea

Below is a an example:

下面是一个例子:

Controller:

控制器:

    if (Session["pageInitCounter"] == null)
    {
        Session["pageInitCounter"] = 1;
    }
    else
    {
        int counter = Convert.ToInt32(Session["pageInitCounter"]);
        counter++;
        Session["pageInitCounter"] = counter;
    }

View:

看法:

@Html.Hidden("pageInitCounter", Session["pageInitCounter"])

Javascript:

Javascript:

alert($("#pageInitCounter").val());