Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.EditorFor and custom format

I got a property in my model which is a DateTime . I would like to get an empty <input /> (instead of one containing '0001-01-01 00:00:00') if the property contains DateTime.MinValue.

Is that possible?

like image 942
jgauffin Avatar asked Jan 07 '11 14:01

jgauffin


1 Answers

One solution I've found working for me was to add strongly typed partial view (for System.DateTime) and put it in Views/Shared/EditorTemplates directory. File DateTime.cshtml looks pretty much like this:

@model System.DateTime
@Html.TextBox("", Model == DateTime.MinValue ? "" : Model.ToShortDateString())

This will however format all your DateTime fields.

More information can be found in this article.

like image 116
k.m Avatar answered Sep 18 '22 02:09

k.m