Asp.Net MVC 2 Html.TextBoxFor指定型号的财产的DateTime格式字符串的最佳方法 [英] Asp.Net MVC 2 Html.TextBoxFor Best way to specify a format string for a DateTime property of model

查看:84
本文介绍了Asp.Net MVC 2 Html.TextBoxFor指定型号的财产的DateTime格式字符串的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于问题的标题解释,什么是指定我的意见通过显示的DateTime 属性的值时,使用的格式的最佳方法 Html.TextboxFor 方法。

As the question title explains, what is the best way to specify a format which my Views use when displaying values of a DateTime property via the Html.TextboxFor method.

默认显示包括日期和时间,我真的只想要显示的日期。

The default display includes the Date and Time where I really only want the Date displayed.

感谢

推荐答案

在您的模型定义添加<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.aspx\"相对=nofollow> DisplayFormatAttribute 以日期时间属性:

In your model definition add the DisplayFormatAttribute to the DateTime property:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }

然后在您查看呼叫:

Then in your View call:

<%=Html.EditorFor(m => m.Date)%>

如果一切顺利的话你应该用在属性定义规定的格式填写值的文本框。

If all goes well you should get a TextBox with the value filled in in the format specified in the attribute definition.

这篇关于Asp.Net MVC 2 Html.TextBoxFor指定型号的财产的DateTime格式字符串的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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