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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.