asp.net-mvc 具有默认日期的 MVC4 日期选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21393870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC4 Date Picker with Default Date
提问by JSK NS
I am using MVC4 with the Razor view engine. My controller is as follows:
我将 MVC4 与 Razor 视图引擎一起使用。我的控制器如下:
public ActionResult Index()
{
return View(new EmployeeIndexViewModel
{
ToDate = DateTime.Now
});
}
My View Model is as follows:
我的视图模型如下:
public class EmployeeIndexViewModel
{
[DataType(DataType.Date)]
public DateTime ToDate { get; set; }
}
On my view I have a simple EditorFor()call:
在我看来,我有一个简单的EditorFor()电话:
@Html.EditorFor(model => model.ToDate)
But still get a default (masked) value in my date picker:
但仍然在我的日期选择器中获得默认(屏蔽)值:


QUESTION:
题:
How can I get today's date (from controller) to display here instead?
如何获取今天的日期(来自控制器)以显示在此处?
回答by JSK NS
My problem was not a jQuery or MVC4 problem as I had initially thought. It has to do with how the HTML5 compatible browser displays the date picker. I was passing the date to the view in the incorrect format.
我的问题不是我最初认为的 jQuery 或 MVC4 问题。它与 HTML5 兼容浏览器如何显示日期选择器有关。我以不正确的格式将日期传递给视图。
I modified my ViewModel to this and now the date populates correctly:
我将我的 ViewModel 修改为这个,现在日期正确填充:
public class EmployeeIndexViewModel
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ToDate { get; set; }
}


回答by Pawe? Bejger
You should do this from jQuery level:
您应该从 jQuery 级别执行此操作:
$( ".selector" ).datepicker( "setDate", yourDate);
For example, if you want to set the today's date:
例如,如果要设置今天的日期:
$( ".selector" ).datepicker( "setDate", new Date());
You can also try adding the DisplayFormatattribute in your view model class to make sure that the date picker will interpret your data format correctly:
您还可以尝试DisplayFormat在视图模型类中添加该属性,以确保日期选择器能够正确解释您的数据格式:
public class EmployeeIndexViewModel
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ToDate { get; set; }
}
回答by Radha Anil K
U can set the Input or Input with type="date" (datetimepicker) using JQuery.
您可以使用 JQuery 使用 type="date" (datetimepicker) 设置输入或输入。
In Razor view
在剃刀视图中
@Html.TextBoxFor(m => m.StartedDate, new { @id = "mydate", @type = "date", @class = "form-control" })
@Html.TextBoxFor(m => m.StartedDate, new { @id = "mydate", @type = "date", @class = "form-control" })
Here is the JS code
这是JS代码
$(document).ready(function () {
var dateNewFormat, onlyDate, today = new Date();
dateNewFormat = today.getFullYear() + '-';
if (today.getMonth().length == 2) {
dateNewFormat += (today.getMonth() + 1);
}
else {
dateNewFormat += '0' + (today.getMonth() + 1);
}
onlyDate = today.getDate();
if (onlyDate.toString().length == 2) {
dateNewFormat += "-" + onlyDate;
}
else {
dateNewFormat += '-0' + onlyDate;
}
$('#mydate').val(dateNewFormat);
});

