0

Can I configure the HTML Helper to display String.Empty if my date is MinValue?

${Html.TextBoxFor(v=>v.Date)}

How can I do this?

Roman
  • 19,581
  • 6
  • 68
  • 84
Aline Bossi
  • 63
  • 1
  • 2
  • 5
  • 2
    I think you have the wrong approach. Instead, use a ViewModel to do this type of formatting/logic. – vidalsasoon Mar 26 '14 at 18:25
  • I agree with @vidalsasoon, the output of the helper is a bunch of html, so you can't control its output without changing the helper or building your own. – RobertTheGrey Mar 27 '14 at 10:00

3 Answers3

0

Why don't you do it in your View?

Caution: Razor syntax

@if(v.Date != DateTime.MinValue)
{
  Html.TextBoxFor(v => v.Date);
}
else {
  //show whatever
}
CharlieBrown
  • 4,143
  • 23
  • 24
0

There are a few similar questions like

If you have full control over the viewmodel, one option would be to make the date field nullable and have the accessor return null if the date is equal to DateTime.MinValue.

If you don't want to change your viewmodel, you can write your own html helper and put the logic to create an empty textbox there. That allows you to keep logic out of your view, where it's difficult to test.

Community
  • 1
  • 1
Roman
  • 19,581
  • 6
  • 68
  • 84
0

Make your property nullable and it won't default to the min date. If that's a database field and you can't do that then you should create a ViewModel property that is and have it compare against the min value and return null in the get{}

Honorable Chow
  • 3,097
  • 3
  • 22
  • 22