user210757
user210757

Reputation: 7386

ASP.NET MVC Razor Ternary with HTML.ActionLink

I am trying to do the answer to this:

How to use ternary operator in razor (specifically on HTML attributes)?

With a Html.ActionLink; something like this:

@(ViewData["page"] == "Page1" ? "Page1" : Html.ActionLink("Page 1", "Page1", "Index"))

Is this possible?

Upvotes: 4

Views: 1426

Answers (1)

SLaks
SLaks

Reputation: 887355

A ternary operation must return the same type from both halves.
You're returning a String on the left, but an IHtmlString on the right.

Change it to

@(ViewData["page"] == "Page1" ? Html.Raw("Page1") : Html.ActionLink(...))

You may also want to move this into an HTML helper extension method.

Upvotes: 6

Related Questions