Change column value at runtime based on condition in mudblazor

157 Views Asked by At

Here is my mudtable

<MudTable Items="Items"  FixedHeader="true" Style="width: 100%; height: calc(100% - 52px);"
          MultiSelection="true" SelectOnRowClick="false" >
    <HeaderContent>
        <MudTh>@ScanRulesPageLocalizer["Name"]</MudTh>
        <MudTh>@ScanRulesPageLocalizer["Rule"]</MudTh>            
    </HeaderContent>
    <RowTemplate>
        <MudTd DataLabel="Name">@context.Name</MudTd>
        <MudTd DataLabel="Rule">
           
            @if (string.IsNullOrEmpty(@context.rule))
            {
                "IDX";
            }
            else
            {
                "RX";
            }
        </MudTd>
        
    </RowTemplate>
</MudTable>

But this is giving me syntax error

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

How to fix this?

1

There are 1 best solutions below

1
On BEST ANSWER

The "IDX"; and "RX"; in the if block are statements with no purpose. If you write the same code in any .cs file. You will get the same compiler warning because nothing is being done with that statement. Hence the error,

CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

<MudTd DataLabel="Rule">
    @if (string.IsNullOrEmpty(context.rule))
    {
        "IDX";
    }
    else
    {
        "RX";
    }
</MudTd>

If you're trying to render different cell data based on the condition. Then you need to either surround the data you want to render in HTML elemenets,

<MudTd DataLabel="Rule">
    @if (string.IsNullOrEmpty(context.rule))
    {
        <div>IDX</div>
    }
    else
    {      
        <div>RX</div>
    }
</MudTd>

Or surround it with an @ symbol to denote that the following statement is a markup string.

<MudTd DataLabel="Rule">
    @if (string.IsNullOrEmpty(context.rule))
    {
        @("IDX")
    }
    else
    {      
        @("RX")
    }
</MudTd>