1

I'm developing in asp.net mvc. I want to have a textbox which is date type, so there's a shortcut that user can click and select a date instead of type in. Meanwhile I want the textbox to display a default date value.

I have tried 2 ways: <input type="date" name="toDate" value="@DateTime.Today.Date.ToShortDateString()" />

@Html.TextBox("date", DateTime.Now.ToShortDateString(), new { type = "date"})

But these two methods generate textbox with 'dd/mm/yyyy' displaying in the box instead of the the default value I set. When I right click on the textbox and inspect element, I can see that there is a value of current date, but it can't show in the textbox!

When I replace the type="date" to type="text", I can see the default value on textbox. But there's no shortcut for me to select a date, it only allows me to type in.

How do I do this to reserve both the shortcut and default value?

tereško
  • 58,060
  • 25
  • 98
  • 150
user2093360
  • 249
  • 3
  • 4
  • 8

3 Answers3

7

I think you want HTML5 date picker. It depends on web browser you are using. Currently, Google chrome and Opera support that.

<input type="date" value="2012-10-10"/>
@Html.TextBox("date", DateTime.Now.ToString("yyyy-MM-dd"), new { type = "date" })
cat916
  • 1,363
  • 10
  • 18
  • The first one is working. But it can't dynamically display the current date. When I try to replace it with a parameter, eg: var x = DateTime.Now.ToShortDateString() , it doesn't work again. The second line of your code returns the same result as mine – user2093360 May 29 '13 at 04:24
0

What happens if you do

@Html.TextBox("date", DateTime.Now.ToString("dd/mm/yyyy"), new { type = "date"})
tsukimi
  • 1,605
  • 2
  • 21
  • 36
  • 1
    a date type textbox which I can select date by click the shortcut, but the default value is dd/mm/yyyy. It doesn't show the real date – user2093360 May 29 '13 at 04:19
0

You need to put this: <input type="date" name="FechIni" value="@DateTime.Now.Date.ToString("yyyy-MM-dd")" />

Pablo
  • 1