c# - 仅显示 DateTime 中的日期

标签 c# asp.net-mvc asp.net-mvc-4

您好,我试图只显示日期时间属性的日期和以货币格式设置的十进制属性的价格。但我一定是做错了什么,因为格式不起作用。这是它的显示方式:

enter image description here

我希望仅使用货币金额显示日期和价格。 这是我的模型代码:

    [Required]
    [DataType(DataType.Date)]
    public DateTime PublicationDate { get; set; }


    [Required]
    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString="{0:c}")]
    public decimal Price { get; set; }

这是我用于 View 的代码:

         <p>
            @Html.LabelFor(model => model.PublicationDate , "Publication Date")
            @Html.TextBoxFor(model => model.PublicationDate, new { @class = "datepicker"})
         </p>
        <p>
            @Html.LabelFor(model => model.Price)
            @Html.TextBoxFor(model => model.Price)
         </p>

我做错了什么?

最佳答案

DateTime Formats日期

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

Double Formats货币

[Required]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString="{0:0.00}")]
public decimal Price { get; set; }

关于c# - 仅显示 DateTime 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355390/

相关文章:

c# - ServiceStack 'Access is denied' 又来了,还有其他问题

c# - 共享点 2010 : How to extract personalized web part property for all users

javascript - Google AdSense JavaScript 导致多个页面加载?

c# - 从服务中检索 Json 并传递给 ApiController

javascript - 无法使用 PUT 方法更新行 - MVC 4?

c# - 在 C# (Visual Studio) 中使用库

c# - 两套 Controller

asp.net-mvc - YSlow 推荐。在 IIS 中,为什么默认情况下不选中启用内容过期

asp.net-mvc - 带有 EF "model first"edmx 模型的 MiniProfiler

c# - ASP.NET Web Api 在选择操作时忽略 RouteParameter.Optional