This is probably a n00b question, I'm trying to get a string variable in my MVC _layout page
so far i have
EDIT:
@{
string myVariable = @Html.Action("GetUserName", "UserController");
}
I need to show the myVariable multiple times on the page. Hence I need to store it somewhere rather than calling the
@Url.Action("GetUserName", "UserController");
multiple times.
Is there a way to do this in mvc?
EDIT 2:
I used
string myVariable = ViewBag.GetUserName()
where
in my BaseController(), I have
public BaseController(){
ViewBag.GetUserName = new Func<string>(GetUserName);
}
[AllowAnonymous]
public string GetUserName()
{
return "Mark ZuckerBerg";
}
When i run my project I get,
Cannot perform runtime binding on a null reference
"User/GetUserName/someUserName? (And its"User", not"UserController"). In any case, why assign it to a c# variable first? - it can just bevar testName = '@Url.Action(...)';var testName='Mark Zuckerberg'Url.Action()is a method for generating a url (it does not actually call it). But why is the value of your UserName not available in the view? You need to provide some more context to understand what you really want to do.