Darkmatter5
Darkmatter5

Reputation: 1249

Is it possible to simplify this if statement?

Here's the code. Is it possible to one-line this code? I know of the (()?:) format, but not sure how to do this in Blazor.

<td>
  @if (piece.PublisherId == null)
  {
    @((MarkupString)$"<i>empty</i>")
  }
  else
  {
    @piece.Publisher.Name
  }
</td>

Upvotes: 0

Views: 181

Answers (2)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30185

In addition to @Henk Holterman's answer:

Using the ?:;

@((MarkupString)(piece.PublisherId is null ? "<i>Empty</i>" : $"{piece.PublisherId}"))

Personally I like to keep my markup code as clean as possible so I would do something like this:

@page "/"

<PageTitle>Index</PageTitle>
<div class="p2">
    DATA : @this.GetDataDiplay(null)
</div>

@code {
    private MarkupString GetDataDiplay(int? value) 
        => new MarkupString(value is null 
            ? "<i>Empty</i>" 
            : $"{value}");
}

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273572

Is it possible to simplify this if statement?

Sure. Replace

@((MarkupString)$"<i>empty</i>")

with

<i>empty</i>"

Is it possible to one-line this code?

Sure.

@if (piece.PublisherId == null) { <i>empty</i> } else { @piece.Publisher.Name }

But a Component might be a better choice.

Upvotes: 0

Related Questions