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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:06:56+00:00 2026-05-21T19:06:56+00:00

I have MVC 3 C# project with Razor engine. What are the ways and,

  • 0

I have MVC 3 C# project with Razor engine. What are the ways and, I guess, the best practices to write dynamic data to the _Layout.cshtml?

For example, maybe I’d like to display user’s name in the upper right corner of my website, and that name is coming from Session, DB, or whatever based on what user is logged in.

UPDATE: I’m also looking for a good practice on rendering certain data into the element of the Layout. For example, if I need to render a specific CSS file depending on the logged-in user’s credentials.

(For the example above, I thought of using Url Helpers.)

  • 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-21T19:06:57+00:00Added an answer on May 21, 2026 at 7:06 pm

    The default internet application created by visual studio use _LogOnPartial.cshtml to do exactly this.

    The user Name value is set in the LogOn action of the HomeController

    Code for _LogOnPartial.cshtml

    @if(Request.IsAuthenticated) {
        <text>Welcome <strong>@User.Identity.Name</strong>!
        [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
    }
    else {
        @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
    }
    

    User.Identity is part of the aspnet Membership provider.

    Code for the _Layout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    </head>
    <body>
        <div class="page">
            <header>
                <div id="title">
                    <h1>Test</h1>
                </div>
                <div id="logindisplay">
                    @Html.Partial("_LogOnPartial")
                </div>
                <nav>
                    <ul id="menu">
                    </ul>
                </nav>
            </header>
            <section id="main">
                @RenderBody()
            </section>
            <footer>
            </footer>
        </div>
    </body>
    </html>
    

    Code for the AccountController Logon Action

    [HttpPost]
            public ActionResult LogOn(LogOnModel model, string returnUrl)
            {
                if (ModelState.IsValid)
                {
                    if (Membership.ValidateUser(model.UserName, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return View(model);
            }
    

    Code for ApplicationViewPage class

    public abstract class ApplicationViewPage<T> : WebViewPage<T>
        {
            protected override void InitializePage()
            {
                SetViewBagDefaultProperties();
                base.InitializePage();
            }
    
            private void SetViewBagDefaultProperties()
            {
                ViewBag.LayoutModel = new LayoutModel(Request.ServerVariables["SERVER_NAME"]);
            }
    
        }
    

    The above code allow me to have a ViewBag.LayoutModel that hold an instance of my LayoutModel class in every page.

    Here is a code for my LayoutModel class

    public class LayoutModel
        {
            public string LayoutFile { get; set; }
            public string IpsTop { get; set; }
            public string IpsBottom { get; set; }
            public string ProfileTop { get; set; }
            public string ProfileBottom { get; set; }
    
            public LayoutModel(string hostname)
            {
                switch (hostname.ToLower())
                {
                    default:
    
                        LayoutFile = "~/Views/Shared/_BnlLayout.cshtml";
                        IpsBottom = "~/Template/_BnlIpsBottom.cshtml";
                        IpsTop = "~/Template/_BnlTop.cshtml";
                        ProfileTop = "~/Template/_BnlProfileTop.cshtml";
                        break;
    
                    case "something.com":
                        LayoutFile = "~/Views/Shared/_Layout.cshtml";
                        IpsBottom = "~/Template/_somethingBottom.cshtml";
                        IpsTop = "~/Template/_somethingTop.cshtml";
                        ProfileTop = "~/Template/_somethingProfileTop.cshtml";
                        break;
                }
            }
        }
    

    Here is the code to the View

    @{
        ViewBag.Title = "PageTitle";
        Layout = @ViewBag.LayoutModel.LayoutFile; 
    }
    @using (Html.BeginForm())
    {
        <span class="error">@ViewBag.ErrorMessage</span>
        <input type="hidden" name="Referrer" id="Referrer" value="@ViewBag.Referrer" />
        html stuff here       
    }
    

    Refer to the following question for more detail. Make sure you modify your web.config as described there: How to set ViewBag properties for all Views without using a base class for Controllers?

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

Sidebar

Related Questions

I have an MVC project that I would like to write in .NET 4.0
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
I have built an ASP.NET MVC 3 web application (with exlusively Razor/cshtml pages) that
I have the following code in a MVC 3 (Razor) project: <div class=user_info id=id_user_info>
I have an Asp.Net mvc 3 project I'm using razor, and need to generate
I have an ASP.NET MVC project with a form. In the Action method that
I have an ASP.NET MVC project and I have a single action that accepts
I have a new ASP.NET MVC project (my first), and I had been running
I have nant set up to build my ASP.NET MVC project and it works
I'm working on a Spring MVC project, and I have unit tests for all

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.