kez kez - 4 days ago 4x
C# Question

session's variables values retrieving in _LayoutPartial in mvc

I'm filling values to a session's like following to retrive those in _LayoutPartial view

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

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

But this is not retrieving data properly . In this _LoginPartial I'm not Referencing any model also


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)