How do @html.displayfor in MVC control the display format of dates

Source: Internet
Author: User
Tags time and seconds

Http://www.tuicool.com/articles/BNVBR3

In SQL Server2005, if a field is defined as datetime, it will be displayed by default in the view of the date and time of the month (e.g. 2013/8/6 13:37:33)

If you only want to display the form of a year and a month, do not time and seconds, then what?

The first method is to set a template for the time display and then call the template where it needs to be displayed.

1. Under the Share folder, create a folder DisplayTemplates

2. Under the DisplayTemplates folder, create a view longdatetime.cshtml

3. Enter the code in the View longdatetime.cshtml

System. DateTime@Model.  Tolongdatestring ()  

Of course, the following sentence can also be changed to @model.toshortdatestring () or other date format.

4, in the need to display the date of the place, by the original

@Html.displayfor (ModelItem = Item. Posttime)

Replaced by

@Html.displayfor (ModelItem = Item. Posttime,"longdatetime")  

This completes the display transformation of the time format. Displayed by the original display (2013/8/6-13:37:33) (August 6, 2013)

The second method: Add DisplayFormat attribute above the model class.

Such as:

[Display (Name = " release Time:")]        [DisplayFormat (dataformatstring = "{0:yyyy mm month DD day}")] public        virtual System.DateTime posttime        {            get;            Set;        } 

displayed, this format is still August 6, 2013.

How do @html.displayfor in MVC control the display format of a date (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.