在 RazorEngine 中格式化可为 null 的小数

Formatting nullable decimal in RazorEngine

RazorEngine.dll 版本是 3.2.

razor 引擎模板(cshtml 文件)中的示例代码:

      @foreach(var row in Model.Trades)
      {            
          <tr>          
             <td>
                @string.Format("{0:N2}",row.Amount)
             </td>
          </tr>
      }     

其中 row.Amount 在交易 class 中定义为: public十进制?数量;

RazorEngine 的堆栈跟踪是:

> System.ArgumentNullException was caught   HResult=-2147467261  
> Message=Value cannot be null. Parameter name: args   Source=mscorlib  
> ParamName=args   StackTrace:
>        at System.String.Format(String format, Object[] args)
>        at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite
> site, T0 arg0, T1 arg1, T2 arg2)
>        at CompiledRazorTemplates.Dynamic.ccdceaafafffaefee.Execute()
>        at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext
> context) in
> c:\Users\Matthew\Documents\GitHub\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateBase.cs:line
> 126
>        at RazorEngine.Templating.TemplateService.Run(ITemplate template, DynamicViewBag viewBag) in
> c:\Users\Matthew\Documents\GitHub\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line
> 608
>        at RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName)
> in
> c:\Users\Matthew\Documents\GitHub\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line
> 439
>        at RazorEngine.Razor.Parse[T](String razorTemplate, T model, String cacheName) in
> c:\Users\Matthew\Documents\GitHub\RazorEngine\src\Core\RazorEngine.Core\Razor.cs:line
> 276

Amount为空时出现错误。 这是一个似乎工作正常的解决方法:

@foreach(var row in Model.Trades)
{            
   <tr>          
      <td>
         @if (row.Amount != null)
         {                
           <text>@string.Format("{0:N4}", row.Amount)</text>
         }
     </td>
  </tr>
}

有什么想法,或者至少有更好的解决方法? 谢谢

编辑:

下面的解决方法更紧凑一些:

         <td>
            @(row.Amount == null ? "" : row.Amount.ToString("N4"))
         </td>

有谁知道 MVC 中的 Razor 是否以相同的方式运行?或者这种行为是 RazorEngine.dll 特有的?

以上错误来自 String.Format()。有趣的是,正如 String.Format() 上的文档所解释的那样,空参数应该导致空字符串。

无论出于何种原因,Razor 选择重载 String.Format(format, Object[]) 来格式化您的字符串。因为你的值是 null.

我创建了一个 small example 来解释这个问题:

int? val = null;

// this one fails:
string template = "Hello @string.Format(\"{0:N4}\", Model.Value)! Welcome to Razor!";
string result = Razor.Parse(template, new { Value = val });

// this (ugly) workaround works:
string template = "Hello @(string.Format(\"{0:N4}{1}\", Model.Value, string.Empty))! Welcome to Razor!";
string result = Razor.Parse(template, new { Value = val });

// this (not so ugly) workaround works as well:
string template = "Hello @(string.Format(\"{0:N4}\", (Object)Model.Value))! Welcome to Razor!";
string result = Razor.Parse(template, new { Value = val });

您的解决方法也可以。

如果这是 Razor 中的错误或功能,我不知道...

编辑 (2):添加了来自 Moe Sisko

的更智能的解决方法

编辑:重写以真正回答问题...