Click here to Skip to main content
14,176,425 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i am retrieving date from database and want to show in input field with date type in view

What I have tried:

controller:
objTournamentEdit.DateOfBirth = Convert.ToDateTime(dt.Rows[0]["DATEOFBIRTH"]);


model:
[DisplayFormat(DataFormatString = "{dd MMM yyyy}")]
        public DateTime TournamentFromDate { get; set; }

view:
@Html.TextBoxFor(model => model.DateOfBirth.ToString("dd/MM/yyyy"), new { @class = "form-control input-sm datepicker", @type = "date", @Value = "", placeholder = "Date of Birth", id = "txtContactDOB", max = "2050-01-01" })
Posted
Updated 26-Feb-19 4:23am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to pass the format string separately to the property expression. You'll need to use the yyyy-MM-dd format, since that's the only format the HTML date input understands.

You'll also want to avoid overriding the value, and avoid hard-coding the maximum date. Depending on your requirements, a sensible maximum date of birth could be DateTime.Today, or DateTime.Today.AddYears(-18), or even DateTime.Today.AddDays(280) if you're registering future births.
@Html.TextBoxFor(model => model.DateOfBirth, "{0:yyyy-MM-dd}", new { @class = "form-control input-sm datepicker", @type = "date", placeholder = "Date of Birth", id = "txtContactDOB", max = DateTime.Today.ToString("yyyy-MM-dd") })
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190525.1 | Last Updated 26 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100