Click here to Skip to main content
15,891,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I developing a web application for a school. I need to save date of birth, admission date etc. Shall I select the date from a list. If possible, how?.
I am writing the code below which I wrote.

In View
C#
 <div class="editor-label">
     <label for="studentDOB">
         Date Of Birth</label>
 </div>
 <div class="editor-field">
     @Html.EditorFor(model => model.studentDOB, new { type = "date" })
     @Html.ValidationMessageFor(model => model.studentDOB)
</div>

In Model
SQL
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
public DateTime studentDOB{ get;  set; }



Any one please help me..
Posted

1 solution

In MVC doesn't have any control for datetime picker. So you should use jQuery UI datepicker or some other third party plugin. It's very easy to implement.

Download jquery plugin from here
http://jqueryui.com/download/[^]
and then add the Scripts and css in head tag like below
JavaScript
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>
</head>

Also you should change your view code like below
JavaScript
  <div class="editor-label">
            <label for="studentDOB">
                Date Of Birth</label>
        </div>
        <div class="editor-field">
@Html.TextBoxFor(model => model.studentDOB, new {id="datepicker"})

            @Html.ValidationMessageFor(model => model.studentDOB)
       </div>

Hope this helps
 
Share this answer
 
v3
Comments
Jineesh TR 31-Oct-14 3:27am    
I think there is no date control in MVC right?. Any way Thank You Jameel. It works fine for me. Thanks a lot.
Jameel VM 31-Oct-14 4:56am    
No date control for MVC.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900