nVelocity - 尝试对十进制属性进行'大于'比较时的模板问题

时间:2010-03-24 06:55:35

标签: templates comparison decimal nvelocity

我有一个简单的对象,它的一个属性是一个名为Amount的小数。当我尝试将此属性作为nVelocity模板的一部分进行比较时,比较总是失败。如果我将属性更改为int类型,则比较工作正常。我需要添加哪些额外的模板才能进行比较吗?

以下是上述模板的示例:

#foreach( $bet in $bets )
<li>
 $bet.Date $bet.Race 
 #if($bet.Amount > 10)
  <strong>$bet.Amount.ToString("c")</strong>
 #else
  $bet.Amount.ToString("c")
 #end
</li>
#end

以下是Bet类:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

I tested this and it worked。看来这是一个在最新版本的NVelocity中不再出现的错误(截至本文撰写时为1.1)。

答案 1 :(得分:0)

不确定

完整的nVelocity模板:

<div>
Bet summary:

<ul>
#foreach( $bet in $bets )
<li>
    $bet.Date $bet.Race 
    #if($bet.Amount > 10)
        <strong>$bet.Amount.ToString("c")</strong>
    #else
        $bet.Amount.ToString("c")
    #end
</li>
#end
</ul>

</div> 

Bet类:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

程序:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Commons.Collections;
using NVelocity.App;
using NVelocity;
using NVelocity.Runtime;

namespace nVelocityTest
{
    public class Program
    {
        private static void Init()
        {
            var props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, @"C:\dev\RnD\nVelocity\nVelocityTest\nVelocityTest\EmailTemplates");
            Velocity.Init(props);
        }

        static void Main()
        {
            Init();

            ICollection<Bet> bet = new Collection<Bet> { new Bet(10, "Banana Race", DateTime.Now), new Bet(15, "Potatoe Race", DateTime.Now) };

            GenerateBetSummaryEmail(bet);
        }

        private static void GenerateBetSummaryEmail(ICollection<Bet> bets)
        {
            var context = new VelocityContext();
            context.Put("bets", bets);

            var writer = new System.IO.StringWriter();

            try
            {
                Velocity.MergeTemplate("BetConfirmationEmailTemplate.vm", context, writer);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem merging template : " + e);
            }

            var outputTest = writer.GetStringBuilder().ToString();
        } 
    }
}

预期outputTest:

        以下是您的投注摘要:      
  • 25/03/2010 9:21:15 AM   香蕉种族$ 10.00
  •     25/03/2010 9:21:15 AM Potatoe Race              $ 15.00
  •   
     

实际outputTest:

        以下是您的投注摘要:      
  • 25/03/2010 9:21:15 AM   香蕉种族$ 10.00
  •     25/03/2010 9:21:15 AM Potatoe Race             $ 15.00
     

如前所述,nVelocity模板中的#if($ bet.Amount&gt; 10)比较失败,即使在第二个投注对象中,bet.Amount的值为15.如果金额更改为输入int,比较按预期工作。