asp.net-corerazor-pagesasp.net-core-5.0

How to use an if else conditional operator in ASP.NET Core Razor Pages


In my ASP.NET Core 5 razor pages application, I have the following piece of code which is working:

@Html.DisplayFor(modelItem => item.IsComplete)

It shows a disabled checkbox as IsComplete is a boolean.

I would like to have text in place of the check box and so I tried the two below unsuccessfully.

@Html.Raw(modelItem => item.IsComplete == true ? "Completed":"")

and

@(modelItem => item.IsComplete == true ? "Completed":"")

The error I get is

error CS1660: Cannot convert lambda expression to type 'object' because it is not a delegate type

How can I show text conditionally?


Solution

  • Wrap the text you want within an @if() condition, like so:

    @if(Model.isCompleted) {
        <p> Completed Transcation </p>
    }