Solved

MVC4 - Display Dates in edit mode

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Counting connections to SQL Server through C# 3 32
Set value of dynamically created checkboxlist 2 32
Hey!! 5 33
Please explain the parts of these 2 LINQ expressions 3 30
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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