在我以前的版本中,我使用了一个日期选择器,默认为null,如下所示;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%:Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) %>
现在我正在使用
@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }))
这不起作用。 当我点击日期字段时,我不再获得弹出窗口。
所以日期字段看起来像;
@Html.EditorFor(model => model.contract.GivenDate)
这是一个DateTime字段。 在我的_Layout中我有;
$(function(){ $(“。datePicker”)。datepicker({showOn:'both',dateFormat:'dd / mm / yy'}); });我也参考;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.4.custom.min.js")" type="text/javascript"></script>
那么我做错了什么?
答案 0 :(得分:1)
以下适用于我:
内部Views\Home\Index.cshtml
:
@model MvcApplication10.Models.FooBar
<!DOCTYPE html>
<html>
<head>
<title>Test DateTime?</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.js")" type="text/javascript"></script>
<link rel="Stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/base/jquery-ui.css" type="text/css" />
<script type="text/javascript">
$(function ()
{
$(".datePicker").datepicker({ showOn: 'both', dateFormat: 'dd/mm/yy' });
});
</script>
</head>
<body>
@Html.EditorFor(model => model.GivenDate)
</body>
</html>
内部Views\Shared\EditorTemplates\DateTime.cshtml
:
@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })
@* you'll notice I also removed an extra ) at the end of the last line *@
最后Models\FooBar.cs
:
public class FooBar
{
public DateTime? GivenDate { get; set; }
}
你可以尝试一下,看看是否有任何差异?
答案 1 :(得分:0)
当我的输入元素没有ID时,我遇到了jQuery日期选择器的问题(我在这里看到你会发生这种情况,因为你在调用Html.TextBox时没有指定名称),也许尝试分配一? E.g。
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker", id = "someUniqueDatePickerId" }))
答案 2 :(得分:0)
我正在使用id而不是类我还包括日期差异功能你可以删除它,如果你想
<script type="text/javascript">
$(document).ready(function () {
$("#StartDate").datepicker();
$('#EndDate').datepicker({
onSelect: function () {
var diff = dateDiff($('#StartDate').datepicker("getDate"), $('#EndDate').datepicker("getDate"));
$('#DateDiff').html("Gece Sayısı:" + diff);
}
});
function dateDiff(startDate, endDate) {
if (endDate && startDate) return (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24);
return "You must complete both dates!";
}
});
<div class="editor-label">
@Html.LabelFor(model => model.StartDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.StartDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EndDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EndDate)
<div id="DateDiff"></div>
@Html.ValidationMessageFor(model => model.EndDate)
</div>
答案 3 :(得分:0)
我找到了答案,尽管这个问题没有线索。 问题是我使用的是javascript库;
的jquery-UI-1.8.4.custom.min.js
当我禁用它时,它有效。 我怀疑有一些库名称冲突导致了这个问题。它适用于jquery-ui-1.8.4.min.js,但现在我使用的是jquery-ui-1.8.10.min.js