2

I am using string.Format in a razor view to format items coming from my viewModel.

It is working fine when I use it like this:

<td>
   <b>@string.Format("{0:c}", item.TotalCreditPaid)</b>
</td>

But, when I try to use it within an if statement it is not displaying anything in the view:

      <td>
          <b>@if (item.AverageTimeToPay != null)
          {
            string.Format("{0} Days", item.AverageTimeToPay);
          }
          </b>
     </td>

I stepped through it and the if is being hit, the item.AverageTimeToPay has a value of 12, but it is not displaying anything.

Any idea why this is happening? Thanks!

5 Answers 5

7

You are missing the @ and parenthesis, change it to this instead.

  <td>
      @if (item.AverageTimeToPay != null)
      {
        <b>@(string.Format("{0} Days", item.AverageTimeToPay))</b>
      }
 </td>
Sign up to request clarification or add additional context in comments.

7 Comments

No point in having an empty <b> rendered, I personally would move the b element inside of the if statement and use that to dictate the rendering of the value
I'll do that. This got it working though. I was putting the @ symbol in front of the string without the parenthesis.
@musefan Correct, I agree but that is not what the question was. :)
@cfly24 No problem. Thanks. :)
@hutchonoid: yeah it's a fair point, though I think it's our duty to improve the code when answering if possible, but it's not a biggy. Also it isn't my downvote incase you was wondering
|
2

You are not rendering your output to Html as string, You can do it in two ways

1)

 <b>
  @if (item.AverageTimeToPay != null)
  {
    @:@string.Format("{0} Days", item.AverageTimeToPay);
  }
  </b>

2)

  @if (item.AverageTimeToPay != null)
  {
    <b>@string.Format("{0} Days", item.AverageTimeToPay)</b>
  }

Check this fiddle

1 Comment

I like the solution with @:@. Thanks! You should leave out the semicolon there, though. It will get rendered to the screen.
1

The code is running, but it's not emitting anything to the page.

This will automatically emit to the page:

@string.Format("{0:c}", item.TotalCreditPaid)

because of how Razor syntax works. Basically the output of the line of code is emitted to the page. However, this is just plain code:

string.Format("{0} Days", item.AverageTimeToPay);

By itself it doesn't do anything, something needs to be done with its output. In your second example, the output of the if block is emitted to the page, but if blocks don't output anything. (The clue here is that semi-colon. It's subtle, but it's kind of an indicator that this is just a server-side line of code and not a Razor output statement.)

All you need to do is tell it to output to the page:

<td>
    @if (item.AverageTimeToPay != null)
    {
        <b>@string.Format("{0} Days", item.AverageTimeToPay)</b>
    }
</td>

Comments

0

The issue is that you aren't breaking out of the code block.

Try this instead:

  <td>
      @if (item.AverageTimeToPay != null)
      {
        <b>@string.Format("{0} Days", item.AverageTimeToPay)</b>
      }
 </td>

or

  <td>
      <b>@if (item.AverageTimeToPay != null)
      {
        @:@string.Format("{0} Days", item.AverageTimeToPay);
      }
      </b>
 </td>

Comments

0

You need to print the string to the page so try:

<td>
    <b>@if (item.AverageTimeToPay != null)
    {
        @string.Format("{0} Days", item.AverageTimeToPay);
    }
    </b>
</td>

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.