string.Format Return value of pure method is not u

2019-09-12 18:10发布

问题:

I am using razor to display decimals from my view model and then trying to format the decimals into currency:

@if (Model != null && Model.Order != null)
{
    foreach (var item in Model.Order.Where(x => x.OrderInStep2 != null))
    {
      String.Format("{0:C}", item.OrderInStep2)
    }
}

I am getting an Return value of pure method is not used warning, but I thought it should still work. However, the formatted item is not displaying at all. It does display when I take away the formatting though. Am I missing something here? Thanks!

回答1:

You need to render the value in a code block like this:

@if (Model != null && Model.Order != null)
{
    foreach (var item in Model.Order.Where(x => x.OrderInStep2 != null))
    {
      <text>@String.Format("{0:C}", item.OrderInStep2)</text>
    }
}


回答2:

You just run the code, and do nothing with the result.

Put the result within text tag:

<text>String.Format("{0:C}", item.OrderInStep2)</text>