gpt4 book ai didi

c# - displayformatattribute 自定义格式化字符串

转载 作者:行者123 更新时间:2023-12-05 07:58:42 25 4
gpt4 key购买 nike

我希望能够在我的 View 模型中标记属性,以便在我的 MVC View 中显示时使用自定义格式进行格式化。

我假设我需要自己的显示格式属性并设置显示格式,但我不确定如何设置此显示格式或在哪里设置。如果我想将数字显示为货币,这很容易,只需在属性的构造函数中将 DataFormatString 设置为“{0:C}”即可。

但是,例如,如果我想屏蔽电子邮件地址(因此删除域名),我将如何使用显示格式属性或数据类型属性来做到这一点?所以它是一个我要转换的字符串字段。我知道还有其他方法,例如自定义显示模板,但我会失去一些内置的 htmlhelper 功能。我只想更改字符串的格式。最好让它像向那些需要屏蔽的字段添加属性一样简单

DataFormatString 似乎无法采用自定义格式化程序?

干杯菲尔

最佳答案

有点类似于:How to make configurable DisplayFormat attribute

public class CustomDisplayFormatAttribute : DisplayFormatAttribute {
public CustomDisplayFormatAttribute() {
//MSDN Custom Date Format string rules:http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
DataFormatString = "{0;dddd dd MMMM YYYY}";
}
}

然后您可以使用它和基本属性,例如ApplyFormatInEditMode,如下所示:

[CustomDisplayFormatAttribute(ApplyFormatInEditMode = false)]
public DateTime? CreatedOn { get; set; }

注意日期有一个服务器与客户端文化设置,http://msdn.microsoft.com/en-us/library/bb882561(v=vs.110).aspx讨论(但不回答)如何在 javascript 中执行此操作。

关于c# - displayformatattribute 自定义格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23778661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com