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 4622720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:53:00+00:00 2026-05-22T02:53:00+00:00

I want to display the user’s screen name in a partial view that is

  • 0

I want to display the user’s screen name in a partial view that is contained in a master page. It is the classic “Log In” / “Welcome John Smith! Log Out” scenario. The user’s screen name is different to the User.Identity.Name and requires a database look up, i.e.

AppUser currentUser = appRepository.GetAppUser(User.Identity.Name);
string screenName = currentUser.ScreenName;

This is not going to work from within a view – there is no reference to ‘appRepository’ and views shouldn’t really be going around making database calls anyway. How should I set this up so that the screen name is passed to the view from the controller?

All my controllers inherit from a BaseController. I tried setting ViewData["CurrentAppUser.ScreenName"] in the constructor of the of the BaseController class, but at that point the User object is not populated yet. (I am using OpenID with DotNetOpenAuth if that is relevant.) I could set it in every action method on every controller, but that would be supremely ugly.

I also tried setting a session variable when the user logs on, but it seems a user can stay logged in even if the session ends. When they come back again they are still logged in, but the session variable is unset.

Any ideas?

  • 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-22T02:53:01+00:00Added an answer on May 22, 2026 at 2:53 am

    I would recommend you using Html.Action. So as always you start by defining a view model:

    public class UserInfoViewModel
    {
        public string ScreenName { get; set; }
    }
    

    then a controller:

    public class UserInfoController: Controller
    {
        private readonly IUsersRepository _repository;
        public UserInfoController(IUsersRepository repository)
        {
            _repository = repository;
        }
    
        // Use this attribute if you want to restrict direct access
        // to this action.
        [ChilActionOnly] 
        public ActionResult Index()
        {
            var model = _repository.GetUserInfo(User.Identity.Name);
            return PartialView(model);
        }
    }
    

    a corresponding partial view:

    @model UserInfoViewModel
    <div>Hello @Html.DisplayFor(x => x.ScreenName )</div>
    

    finally in your master page (no matter which controller was used to render the view) include the partial:

    @Html.Action("Index", "UserInfo")
    

    or (Html.RenderPartial which does the same but doesn’t return the result but writes it directly to the output stream):

    @{Html.ActionPartial("Index", "UserInfo");}
    

    A further improvement of this in order to avoid hitting the database on each request for fetching the user screen name would be to cache it somewhere: session, cookie, …

    So no need of base controllers and ViewData. The logic of getting the user screen name is completely separate from the main MVC pipeline and embedded as a widget.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have master page in asp.net.i want to display user in master page but
I have a TextBlock that I want to display a user name, and email
I want to display a menu that changes according to the user group of
I have some user-submitted variables that I want to display in a different part
I want to display the User's name in email. I have successfully sent an
I want to display to the user a message that says please enable JavaScript
I am working on a windows application where i want to display orkut user
i want to display a msg to the user (msg box or Toast) when
I want to suppress the web browser's default tooltip display when a user hovers
I want to put facility to display distance between two places selected by user.

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.