?
Solved

MVC4 - Display Dates in edit mode

Posted on 2014-01-08
1
Medium Priority
?
279 Views
Last Modified: 2014-02-18
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
Comment
Question by:elmbrook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 total points
ID: 39767657
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question