Solved

MVC4 - Display Dates in edit mode

Posted on 2014-01-08
1
256 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

22 Experts available now in Live!

Get 1:1 Help Now