简体   繁体   English

在ASP.NET MVC的_LayoutPartial中检索会话的变量值

[英]session's variables values retrieving in _LayoutPartial in asp.net mvc

I'm filling values to a session's like following to retrive those in _LayoutPartial view 我正在像下面那样填充会话的值,以检索_LayoutPartial视图中的值

if (userdata != null)
{
    Session["userdata"] = new SampleViewModel { FirstName = userdata.UserFirstName, LastName = userdata.UserLastName };
    FormsAuthentication.SetAuthCookie(loginmodel.UserName, false);
    return RedirectToAction("Dashboard", "Home", new { username = loginmodel.UserName });
}

I want to retrive those values in _LayoutPartial View , So I tried somethin like following 我想在_LayoutPartial View中检索这些值,所以我尝试了以下操作

<a class="sa">
    (@Session["userdata"] as ProjectName.Models.SampleViewModel).FirstName 
    (@Session["userdata"] as ProjectName.Models.SampleViewModel).LastName
</a>

But this is not retrieving data properly . 但这不能正确检索数据。 In this _LoginPartial I'm not Referencing any model also 在此_LoginPartial中,我也没有引用任何模型

You have your parenthesis in the wrong spot, and you need a double set - one for the casting (inner set) and one for the razor execution (outer set). 您的括号放在错误的位置,并且需要一个双重套-一个用于铸造(内部套),另一个用于剃刀执行(外部套)。 It needs to be 它必须是

@((Session["userdata"] as ProjectName.Models.SampleViewModel).Name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM