Solved

MVC4 - Display Dates in edit mode

Posted on 2014-01-08
1
259 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
1 Comment
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now