displayformatattribute自定义格式的字符串 [英] displayformatattribute to custom format a string

查看:1450
本文介绍了displayformatattribute自定义格式的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC视图中显示时,我希望能够在我的视图模型,以纪念性质使用自定义格式进行格式化。

I want to be able to mark properties in my viewmodel to be formatted using a custom format when displayed in my MVC view.

我想我需要我自己的displayformatattribute并设置displayformat,但我不知道该如何设置displayformat或在哪里。如果我要显示一个数字作为货币很容易,只需设置DataFormatString为{0:C}中的属性的构造函数

I assume I need my own displayformatattribute and set the displayformat but I am not sure how to set this displayformat or where. If I want to show a number as currency it is easy, just set DataFormatString to "{0:C}" in the constructor of the attribute.

但是,如果比如我要屏蔽的电子邮件地址(以便删除域名),我会怎么做这使用一个displayformatattribute或者一个datatypeattribute?
所以这是我想改变一个字符串字段。
我知道有其他方法如自定义显示模板,但我会失去一些内置的HtmlHelper功能。我只是想改变字符串没有其他的格式。和preferably使它简单,只要添加属性给那些需要屏蔽领域

But if for example I want to mask email addresses (so remove the domain name), how would I do this using either a displayformatattribute or maybe a datatypeattribute? So it is a string field that I want to transform. I know there are other approaches e.g. custom display templates but then I would lose some of the inbuilt htmlhelper functionality. I just want to change the format of the string nothing else. And preferably make it as simple as adding an attribute to those fields that need masking

在DataFormatString似乎并不能够采取自定义格式?

The DataFormatString doesn't seem to be able to take a custom formatter?

欢呼
菲尔

推荐答案

Somewhate类似于:<一href=\"http://stackoverflow.com/questions/13576571/how-to-make-configurable-displayformat-attribute\">How使配置DisplayFormat属性

Somewhate similar to : 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,像这样:

Then you can use it and the base attributes e.g. ApplyFormatInEditMode, as so:

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

注的日期还有服务器VS客户文化背景,的 http://msdn.microsoft.com/en-us/library/bb882561(v = vs.110)的.aspx 讨论(但是没有接听)如何在JavaScript做到这一点。

Note for dates there is a server vs client Culture setting, http://msdn.microsoft.com/en-us/library/bb882561(v=vs.110).aspx discusses (but doesn't answer) how to do this in javascript.

这篇关于displayformatattribute自定义格式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆