• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

MVC4 - Display Dates in edit mode

Hi

This is a 2 part question on dates.

1. Is there a way to display all valid dates in a particular format (dd/MM/yyyy)

2. Show 01/01/1900 as a blank date

My database contains a lot of date fields and rather than set each individual date to the required format, I would like to be able to do a blanket cover formatting of dates with empty dates shown as a blank date.
0
elmbrook
Asked:
elmbrook
1 Solution
 
Ioannis ParaskevopoulosCommented:
Hi...

You may actually make use of DisplayTemplates.

You will have to create a folder called DisplayTemplates in your Views/Shared folder.
Right click on the newly created folder and select 'Add View'.
Name the view 'DateTime' and make sure you choose it to be a 'Partial View'

In the code of the view you may add the following:

@model DateTime
@Html.Label("",(Model.ToString("dd/MM/yyyy")))

Open in new window


This will make each DisplayFor which is a DateTime to be displayed in the requested format.

I have not added code for the check if the date is 01/01/1900, so if you need help with that, just call...

Keep in mind that you may do this for various Data Types, such as String. You only have to make a Template named after the Data type.

You can also do that for other DataTypes but it needs some more coding. If you have a custom type, you may create a template, such as CustomTypeTemplate and then in the model you may use a data annotation to define which template to use:

public class MyCustomModel
{
        [UIHint("CustomTypeTemplate")]
        public CustomType Custom {get;set;}
}

Open in new window


You may also define a folder 'EditorTemplates' in 'Views/Shared' for EditorFor helpers.

Good Luck,
Giannis
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now