I have the following code in my HomeController’s Index action, and the code below it in my layout. When I call up the Home/Index view, the UserName property isn’t set, but I know it is set in my controller, as I examine the value with a breakpoint.
Controller:
public ActionResult Index()
{
ViewData.Add("UserName", User.Identity.Name);
return View();
}
View:
@{
if (User.Identity.IsAuthenticated)
{
<text>Hello </text>
@ViewBag.UserName <text> - </text>
@Html.ActionLink("Sign Out", "LogOff", "Account", null, new { style = "font-weight: bold;" })
}
}
EDIT: When I try the following view code, as suggested in answers below, I get a the compilation error: ‘System.Web.Mvc.HtmlHelper’ has no applicable method named ‘Display’ but appears to have an extension method by that name.
I wonder if this is not some side effect of my nested layouts? The layout for the view cited is _ThreeColumn, and the layout for the latter is _Layout, set as default in _ViewStart.
First off, you should consistantly use viewbag or viewdata in both your controller and view, as it makes your code easier to understand.
The real problem lies with
@Html.Displayyou dont need the @ symbol again. It should be as follows: