Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core model-view-controller razor

How to render a string to Layout.cshtml file?

发布于 2020-03-27 15:45:25

I have a method that returns a string in HomeController.

public string MyLocation()
{
    return "NYC";
}

I am trying to display the returned string in Nav Item of my _Layout.cshtml file inside <p> tag.

<li>
    <a href="#"> <p font-size:14px">  </p> </a>
</li>
Questioner
zombie551
Viewed
76
Prajakta Kale 2020-01-31 16:32

You can not send data to _Layout page ie.(Master page).

Store your location in the session variable and access it in master page as follows:

public string MyLocation()
   {
    Session["MyLocation"]="NYC";
    return "NYC";
   }

and _Layout Page will be:

     <li>
         <a    href="#"> 
            <p font-size:14px">  
                 @HttpContext.Current.Session["MyLocation"].ToString()   
            </p>
         </a> 
    </li>

This might help you.