This is probably a simple oversight but I’m not seeing the problem so I thought I’d ask for some quick help. I’m somewhat new to MVC also (and Razor) so that might have something to do with it also. Basically, here’s what’s in my Razor View that renders some list items for a navigation bar. I’m just trying to set a class of “selected” on the element if (according to the Controller name) it’s the page being requested.
<li>@{ if(Html.ViewContext.RouteData.Values["controller"].ToString() == "AdminHome")
{
Html.ActionLink("Admin Home", "Index", "AdminHome", null, new { @class = "selected" });
}
else{
Html.ActionLink("Admin Home", "Index", "AdminHome");
}
}
</li>
The result I’m getting is just an empty list item element: <li></li>
Any ideas what I’m doing wrong? Is it just a syntax issue?
You need to prefix
@beforeHtml.ActionLink. Otherwise MVC treats this as a ordinary method call not a method that outputshtml.