[英]SmartFormat.NET nested placeholders in List format error
I am trying to test out the functionality of SmartFormat.NET , and I am having trouble trying to format a list of view model items. 我正在尝试测试SmartFormat.NET的功能,并且在尝试格式化视图模型项列表时遇到麻烦。 According to this exchange , what I am trying to accomplish should be possible with nested placeholders. 根据这次交流 ,嵌套的占位符应该可以实现我要完成的任务。
Here is the template I am using: 这是我正在使用的模板:
var smartTemplate = @"
<div>This is the title</div>
<div>model name: {Name}</div>
<div>model description: {Description}</div>
<div>model rating: {Rating}</div>
{ItemList:
<div>{NestedName} has number equal to {Number}</div>
}";
And my view model(s): 和我的视图模型:
public class SimpleTestVM
{
public string Name { get; set; }
public string Description { get; set; }
public int Rating { get; set; }
public NestedSimpleVM[] ItemList { get; set; }
}
public class NestedSimpleVM
{
public string NestedName { get; set; }
public int Number { get; set; }
}
Then to format the data I initialize a view model with a list of a few items and then use the following code: 然后,要格式化数据,我初始化了一个由几个项目组成的列表的视图模型,然后使用以下代码:
Smart.Default.AddExtensions(new ListFormatter(Smart.Default));
Smart.Default.AddExtensions(new ReflectionSource(Smart.Default));
var smartResult = Smart.Format(smartTemplate, model);
On the call to Format
, I get the following error: 在调用Format
,出现以下错误:
Error parsing format string: Error parsing format string: Could not evaluate the selector "NestedName" at 165
{... includes the template here...}
Digging into the source code it seems that SmartFormat thinks the NestedName
selector was not handled, and that is why it is throwing an error. 深入研究源代码,似乎SmartFormat认为未处理NestedName
选择器,这就是为什么它引发错误。 I can't figure out why it does this; 我不知道为什么要这么做。 as far as I can tell it follows the syntax correctly. 据我所知,它正确地遵循了语法。
With some more poking around through the source, I found the issue. 通过更多地查看源代码,我发现了问题。 The ListFormatter requires a "|" ListFormatter需要一个“ |” character to denote the separator for the formatted items, so I changed my format to: 字符来表示格式化项的分隔符,因此我将格式更改为:
var smartTemplate = @"
<div>This is the title</div>
<div>model name: {Name}</div>
<div>model description: {Description}</div>
<div>model rating: {Rating}</div>
{ItemList:
<div>{NestedName} has number equal to {Number}</div> | }
";
This works fine. 这很好。 Now to figure out how to conditionally display items based on their properties. 现在了解如何根据其属性有条件地显示项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.