razorengine电子邮件模板(无法编译)

时间:2014-06-25 11:04:45

标签: asp.net asp.net-mvc razor razorengine

我正在尝试使用此行解析我的电子邮件模板。我正在使用razorengine

 public string Converttemplate(Guid RecordId, string str,string Redirecto)
    {          
        string str1 = string.Empty;
        if (Redirecto == "Quotation")
        {
            var quot = QuotationService.GetQuotationForDetails(RecordId);

            str1 = RazorEngine.Razor.Parse<QuotationForDetailsViewModel>(str, quot);
        }

        return str1;
    } 

基本上str包含这样的东西

Dear @Model.CustomerId
Your QuotationId @Model.QuotationNo is created , 
Your Sincerely Admin

这将正确分析,因为@ Model.CustomerId和@ Model.QuotationNo都包含在 quot 模型中。

但如果我将@ Model.CustomerId替换为“模型模型中不存在的@ Model.CustomerName,那么它会给无法编译错误?< / p>

我确信这将在框架中处理!!我该如何解决这个问题?

修改

public QuotationForDetailsViewModel GetQuotationForDetails(Guid QuotationId)
    {
        var query = (from quot in _context.Quotations
                     where quot.QuotationId == QuotationId
                     select new QuotationForDetailsViewModel
                     {
                         AgencyName = quot.Agency.AgencyName,
                         AgencyScopeName = quot.QuotationScope.ScopeName,
                         CustomerId=quot.CustomerId,
                         CustomerName = quot.Customer.CompanyName,                    
                         PortName = quot.Port.PortName,
                         Vessel=quot.Vessel.CurrentName,
                         VesselId=quot.VesselId,
                         PortId = quot.Port.PortId,
                         IsJvFull=quot.isJvFull,                    
                         ETA = quot.ETA,
                         NRT = quot.NRT,
                         GRT = quot.GRT,
                         isJobInitiated = quot.isJobInitiated,
                         isCancelled = quot.isCancelled,
                         IsJvCreated=quot.IsJVCreated,
                         IsFullyTaxable=quot.isFullyTaxable,
                         IsFinalInvoiceCreated=quot.FinalInvoiceCreated,
                         IsInitialInvoiceCreated=quot.initialInvoiceCreated,
                         PerDay =quot.PerDay,
                         TaxRate= quot.TaxRate,
                         TotalPkr = quot.TotalAmountPKR,
                         TotalUSD = quot.TotalAmountUSD,
                         RateTerminal = quot.RateTerminalId,
                         QuoteDate = quot.QuoteDate,
                         Quantity = quot.Quantity,
                         UpFrontCriteria=quot.UpfrontCriteria,
                         UpFrontFee=quot.UpfrontFee,
                         UpFrontPercentage=quot.UpfrontPercentage,
                         UpFrontCurrencyId=quot.UpfrontCurrencyId,
                         QuotationNo = quot.QuotationNo,
                         QuotationId=quot.QuotationId,
                         ExchangeRate=quot.ExchangeRate,
                         TransactionExchangeRate=quot.TransactionExchangeRate,
                         Terminal = quot.QuotationBerths.Select(m => m.Terminal.TerminalName).ToList(),
                         Purposeofcallcargo = quot.QuotationPurposeOfCalls.Select(m => m.PurposeOfCall.PurposeOfCallName).ToList(),
                         CargoType = quot.QuotationCargoCategories.Select(m => m.CargoCategory.CargoCategoryName).ToList(),
                         PortServiceTariffIncluded = (from service in quot.QuotationServices
                                                      select new PortServiceDetailViewModel
                                                      {
                                                          Id = service.QuotationServicesId,
                                                          Isstage=service.InvoiceStage,
                                                          ServiceId = service.PortServicesTariffId,
                                                          ServiceName = service.PortServicesTariff.ServiceName,
                                                          ServiceCriteria = service.PortServicesTariff.PortServiceCriteria.CriteriaName,
                                                          ItemBasis = service.PortServicesTariff.PortServiceItemBasi.ItemBasisName,
                                                          ServiceCriteriaId = service.PortServicesTariff.PortServiceCriteria.CriteriaId,
                                                          ItemBasisId = service.PortServicesTariff.PortServiceItemBasi.ItemBasisId,
                                                          Rate = service.Rate,
                                                          Amount = service.Amount,
                                                          TotalPKR=service.AmountPKR,
                                                          TotalUSD=service.AmountUSD,
                                                          IsCancelled= service.IsCancelled,
                                                          InvoicedAmountUSD=service.InvoicedAmountUSD,
                                                          InvoicedAmountPKR= service.InvoicedAmountPKR,
                                                          ActualAmountPKR=service.ActualAmountPKR,
                                                          ActualAmountUSD=service.ActualAmountUSD,   
                                                          VendorDate=service.VendorDate,
                                                          PrincipleDate=service.PrincipleDate,
                                                          IsPrinciple = service.Customer.PartyTypeId == 1 ? true : false,
                                                          BilledTo =service.BilledTo,
                                                          BillerName=service.Customer.CompanyName,
                                                          VendorId=service.VendorGUID,
                                                          VendorName=service.Customer1.CompanyName,                                                                
                                                          Currency = service.PortServicesTariff.Currency.CurrencyName
                                                      }).ToList()

                     }).FirstOrDefault();
        return query;

1 个答案:

答案 0 :(得分:0)

如果你想支持'无效'属性名称/方法/事件......你需要使用dynamic并编写自己的包装器:

public class MyWrapper : DynamicObject
{
    private dynamic _inner;
    public MyWrapper(dynamic inner) {
        _inner = inner;
    }

    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        try {
            // Dynamically invoke binder.Name on _inner (you can use reflection for example)
            // Another way is to use https://github.com/ekonbenefits/impromptu-interface:
            result = new Invocation(InvocationKind.Get, binder.Name).InvokeWithStoredArgs(_inner);
        } catch {
            // RazorEngine should ignore this.
            result = "";
        }
        return true;
    }
}

然后你只需要将该包装器提供给RazorEngine。您可以覆盖DynamicObject的更多方法并将其转发以支持更多方案(但您应该明白这一点)。