简体   繁体   English

如何格式化要在DisplayFor中使用的MVC模型类中的字符串属性

[英]How to format string property in MVC model class to be used in DisplayFor

Okay, I have been looking for a resolution for this simple task all day. 好的,我整天都在寻找解决方案。

I have an mvc model class which has a BIC property and a NationalNumber property. 我有一个MVC模型类,它具有BIC属性和NationalNumber属性。 Bother are string values. 麻烦的是字符串值。 I want to use a DisplayFor helper to render the property on the view BUT, with a formatting applied. 我想使用DisplayFor助手在视图BUT上渲染属性,并应用格式。

for completeness: the formatting for the NationalNumber is '00.00.00-000.00' , the formatting for BIC is AAAA BB CC 为了完整性:NationalNumber的格式为'00 .00.00-000.00',BIC的格式为AAAA BB CC

I tried annotating my properties with a DisplayFormat Attribute, but that only seems to work with DateTimes, numeric values, etc,.. 我尝试使用DisplayFormat属性注释属性,但似乎只能与DateTimes,数字值等一起使用。

[DisplayFormat(DataFormatString = "{0:##.##.##-###.##}")]

Then I looked at creating a custom DisplayFormat attribute , but that also works with patterns that apply to DateTimes, numeric values, etc.. You still need to suppy a DataFormatString value in the constructor of your custom attribute. 然后,我研究了如何创建一个自定义DisplayFormat属性 ,但它也适用于适用于DateTimes,数字值等的模式。您仍然需要在自定义属性的构造函数中提供一个DataFormatString值。 But the filter does not seem to work with strings! 但是过滤器似乎不适用于字符串!

For the moment I ended up doing the markup clientSide(with a mask plugin) but that's not really what I want! 目前,我最终做了标记clientSide(带有遮罩插件),但这并不是我真正想要的!

To summarize: I want to use @Html.DisplayFor(x=>x.BicNumber) and have it rendered with a custom formatting, preferrably annotaded on my viewmodel, with BicNumber being a string. 总结一下:我想使用@Html.DisplayFor(x=>x.BicNumber)并以自定义格式对其进行呈现,最好在我的viewmodel上进行注释,其中BicNumber是一个字符串。

Thanks in advance 提前致谢

If your NationalNumber and BIC were separate data types, you could create display templates , store them under ~/Views/Shared/DisplayTemplates/NationalNumber.cshtml and ~/Views/Shared/DisplayTemplates/BIC.cshtml , and it would then automatically work as you want. 如果您的NationalNumberBIC是单独的数据类型,则可以创建显示模板 ,将它们存储在~/Views/Shared/DisplayTemplates/NationalNumber.cshtml~/Views/Shared/DisplayTemplates/BIC.cshtml ,然后它将自动以你要。

If you want to keep these properties as strings, the automatic approach won't work for you because a display template created for string.cshtml would affect all strings in the project. 如果要将这些属性保留为字符串,则自动方法将对您不起作用,因为为string.cshtml创建的显示模板将影响项目中的所有字符串。

So create a display template named ~/Views/Shared/DisplayTemplates/NationalNumber.cshtml , where you would manually output parts of the number: 因此,创建一个名为~/Views/Shared/DisplayTemplates/NationalNumber.cshtml的显示模板,您将在其中手动输出部分数字:

@model string
@String.Format("{0}.{1}.{2}-{3}.{4}", Model.Substring(0, 2), Model.Substring(2, 2), Model.Substring(4, 2), Model.Substring(6, 3), Model.Substring(9, 2))

(note the capitalized String , it is important ) and specify it explicitly: (注意大写的String这很重要 ),并明确指定它:

@Html.DisplayFor(x=>x.NationalNumber, "NationalNumber")

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

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