4

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?

Community
  • 1
  • 1
user210757
  • 6,996
  • 17
  • 66
  • 115

1 Answers1

6

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964