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>
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.
it says. the name session doesnt exist in current context.
Check System namespace imported or not. It should be work
i think this method is for .NET framework, i am using core and its not working.
stackoverflow.com/a/50425129/10759381 check this. It may help you