简体   繁体   English

在if语句中时string.Format在C#剃刀中不起作用

[英]string.Format not working in C# razor when inside if statement

I am using string.Format in a razor view to format items coming from my viewModel. 我在剃刀视图中使用string.Format来格式化来自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: 但是,当我尝试在if语句中使用它时,它在视图中未显示任何内容:

      <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. 我逐步浏览了该项目,如果被击中, item.AverageTimeToPay的值为12,但未显示任何内容。

Any idea why this is happening? 知道为什么会这样吗? Thanks! 谢谢!

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>

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. 因为Razor语法是如何工作的。 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. 在第二个示例中, if的输出被发送到页面,但是if块不输出任何东西。 (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.) (这里的线索是分号。虽然很微妙,但这只是一种指示,它只是服务器端的一行代码,而不是Razor输出语句。)

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>

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>

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>

You are not rendering your output to Html as string, You can do it in two ways 您没有将输出呈现为Html字符串,可以通过两种方式实现

1) 1)

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

2) 2)

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

Check this fiddle 检查这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM