Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:37:37+00:00 2026-05-27T17:37:37+00:00

In the partial view I have to access Session variable LoginInfo which holds a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:37:38+00:00Added an answer on May 27, 2026 at 5:37 pm

    What am I doing wrong?\

    Many things. The first thing you did wrong is the Razor syntax. It should be:

    @{ LoginInfo info = (LoginInfo)Session["LoginInfo"]; }
    

    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:

    public class MyViewModel
    {
        public LoginInfo LoginInfo { get; set; }
    }
    

    then you have a controller action:

    public ActionResult Foo()
    {
        var model = new MyViewModel();
        model.LoginInfo = (LoginInfo)Session["LoginInfo"];
        return View(model);
    }
    

    then you have a strongly typed view in which you use the view model:

    @model MyViewModel
    <div>Hello @Model.LoginInfo.FirstName</div>
    

    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):

    [ChildActionOnly]
    public ActionResult LoginInfo()
    {
        var model = (LoginInfo)Session["LoginInfo"] ?? new LoginInfo();
        return PartialView(model);
    }
    

    and then you define a corresponding partial:

    @model LoginInfo
    <div>Hello @Model.FirstName @Model.LastName</div>
    

    and finally in your _layout you can include this widget somewhere:

    @Html.Action("LoginInfo", "SomeController")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
I have the following code in a partial view (using Spark): <span id=selectCount>0</span> video(s)
I have a partial view that I want to basically take care of itself
I have a partial view that is shared between two controllers and I'm trying
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I have this in my partial view: <tr> <% for (int currentDay = 0;
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
if i have created a view model and have a partial form that is
How can I have a view render a partial (user control) from a different
I'm using MVC2 with VS2010 I have a view that has two partial views

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.