In the partial view I have to access Session variable LoginInfo which holds a LoginInfo object. I tried the following:
@LoginInfo info = @(LoginInfo)Session["LoginInfo"]
But it gives me compiler errors. What am I doing wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Many things. The first thing you did wrong is the Razor syntax. It should be:
But that’s just the syntax. You have a far bigger problem. You have a design problem. You are violating the MVC pattern.
A view should not try to fetch any data. A view uses data that is passed to it from the controller action under the form of a view model.
So you define a view model:
then you have a controller action:
then you have a strongly typed view in which you use the view model:
But in this case (given the name of the class
LoginInfo) I suppose that you are trying to display some common widget on all views. That would be a great candidate for using the Html.Action helper.So you define controller action that will fetch this information from some data source (Session in your case):
and then you define a corresponding partial:
and finally in your _layout you can include this widget somewhere: