[英]jQuery UI datepicker not working with asp.net mvc
我正在使用带有Jquery日期选择器的ASP.Net MVC。
这是我的模特班
public class myViewModel
{
public DateTime? FromDate {get;set;}
public DateTime? ToDate {get;set;}
}
这是我的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" })
和JQuery
<script>
$(function () {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
现在的问题是,无论何时我的页面加载,默认日期都不会显示在文本框中,而是在两个文本框中都显示“ {0:MM / dd / yyyy}”
那么,如何在FromDate和ToDate文本框中显示并绑定当前日期值?
谢谢
用这个,
<script>
$(function () {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
setDate : new Date()
});
});
</script>
确保模型中包含以下using语句
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
更改模型如下
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Date { get; set; }
您的JQuery是
<script>
$(function () {
$(".datepicker").datepicker({ dateFormat: "MM/dd/yy", changeMonth: true, changeYear: true });
});
</script>
而且View Class是
@Html.TextBoxFor(model => model.FromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })
@Html.TextBoxFor(model => model.ToFromDate, "{0:MM/dd/yyyy}", new { id = "datepicker" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.