I am working with ASP.Net MVC with Jquery Date picker.
here is my model class
public class myViewModel
{
public DateTime? FromDate {get;set;}
public DateTime? ToDate {get;set;}
}
here is my View Class
@model testApp.myViewModel
@Html.TextBox("FromDate","{0:MM/dd/yyyy}", new { @class = "datepicker" })
@Html.TextBox("ToDate","{0:MM/dd/yyyy}", new { @class = "datepicker" })
and JQuery
<script>
$(function () {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
Now the issue is whenever my page is load default date does not display in textbox instead "{0:MM/dd/yyyy}" display in both textbox
So how can I display and bind current date value in my FromDate and ToDate textbox?
Thanks
use this,
<script>
$(function () {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
setDate : new Date()
});
});
</script>
Make sure the following using statements are included in your Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Change Model as follows
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Date { get; set; }
Your JQuery is
<script>
$(function () {
$(".datepicker").datepicker({ dateFormat: "MM/dd/yy", changeMonth: true, changeYear: true });
});
</script>
And View Class is
@Html.TextBoxFor(model => model.FromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })
@Html.TextBoxFor(model => model.ToFromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.