使用常数在MVC定义属性格式 [英] Using constant for defining property format in MVC
问题描述
在我的MVC应用我的日期时间数据类型的许多属性和我定义在此数据类型的每个新属性DataFormatString的定义如下:
In my MVC application I have many properties of DateTime datatype and I define DataFormatString on every new property in this datatype is defined as below:
型号:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime StartDate{ get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime EndDate{ get; set; }
而不是这个,我认为还有另一种选择通过创建一个包含常数或使用web配置,等于是一个新的类,定义这些DataFormatStrings只是一个地方,一次在这种情况下,什么是最好的方式用常量值,即日期格式,等我用全球化串在我的web.config,但是我不知道在web.config中定义日期DataFormatStrings为好。任何帮助将是AP preciated。
Instead of this, I think there is another option to define these DataFormatStrings for just one place and at once by creating a new class containing constant value or using web config, etc. So, in this case what is the best way to use constant values i.e. date format, etc. I use globalization string on my web.config, but I am not sure defining date DataFormatStrings in web.config as well. Any help would be appreciated.
推荐答案
我想选择一个自定义属性
I'd opt for a custom attribute
public class ShortDateFormatAttribute : DisplayFormatAttribute
{
public ShortDateFormatAttribute()
{
DateFormat = "{0:dd/MM/yyyy}";
ApplyFormatInEditMode = true;
}
}
....
[ShortDateFormat]
public DateTime StartDate { get; set; }
[ShortDateFormat]
public DateTime EndDate { get; set; }
这篇关于使用常数在MVC定义属性格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!