Reputation: 1369
Consider the following code:
@{
if ((string) ViewContext.RouteData.DataTokens["area"] == "Guest"){
<text>areaNav_link--selected</text>
}
}
How would I go about removing that if statement?
Upvotes: 2
Views: 460
Reputation: 33306
I prefer to use custom html helpers to remove logic.
As an example, you would call it as follows:
@Html.WelcomeText()
The logic could be moved into the helper as follows:
public static MvcHtmlString WelcomeText(this HtmlHelper htmlHelper)
{
var text = string.Empty;
var areaName = ViewContext.RouteData.Values["area"].ToString();
if (areaName == "Guest")
{
text = "Hello Guest";
}
return new MvcHtmlString(text);
}
This way you could re-use it throughout the site with a single line of Razor code.
An alternative would be to create a TagHelper.
Upvotes: 2
Reputation: 2756
If you are thinking about inline if statement then you can do something like that:
@((string) ViewContext.RouteData.DataTokens["area"] == "Guest" ? "areaNav_link--selected" : string.Empty)
Upvotes: 0