asp.NETMVC3Razor&;jQueryUI日期选择器
我想在我的datetime文本框上显示jquery日期选择器 我在Views/Shared/EditorTemplates中创建了DateTime.cshtml文件添加了以下内容:asp.NETMVC3Razor&;jQueryUI日期选择器,jquery,asp.net-mvc-3,razor,Jquery,Asp.net Mvc 3,Razor,我想在我的datetime文本框上显示jquery日期选择器 我在Views/Shared/EditorTemplates中创建了DateTime.cshtml文件添加了以下内容: @model Nullable<System.DateTime> @if (Model.HasValue) { @Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model.Value), new { @class = "date" }
@model Nullable<System.DateTime>
@if (Model.HasValue)
{
@Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model.Value), new { @class = "date" })
}
else
{
@Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", ""), new { @class = "date" })
}
@{
string name = ViewData.TemplateInfo.HtmlFieldPrefix;
string id = name.Replace(".", "_");
}
<reference path="jquery-1.4.4.js" />
<reference path="jquery-ui.js" />
<script type="text/javascript">
$(document).ready(function () {
$(".date").datepicker
({
dateFormat: 'dd/mm/yy',
showStatus: true,
showWeeks: true,
highlightWeek: true,
numberOfMonths: 1,
showAnim: "scale",
showOptions: {
origin: ["top", "left"]
}
});
});
</script>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd}", Model.HasValue ? Model : DateTime.Today), new { @class = "dp"})%>
你有很多问题。首先,您需要去掉模板中的引用标记,它们是不合法的。这不是使用引用标记的方式 其次,您不希望包含script标记,因为如果页面上有多个DateTime,它将多次执行代码 接下来,您不能这样做:
@Html.EditorFor(model => model.Birthdate, null, new { @class = "date"})
@Html.EditorFor(model => model.Birthdate)
编辑器模板不接受html属性。您还指定了一个空模板(第二个参数),因此它根本不使用任何模板,即使您的模板名为DateTime.cshtml,也会覆盖模板选择
只要这样做:
@Html.EditorFor(model => model.Birthdate, null, new { @class = "date"})
@Html.EditorFor(model => model.Birthdate)
另外,在属性的类型定义中去掉global::,而是确保在using列表中包含System。不确定global::是否会干扰类型解析。好的,我也遇到了类似的问题,这就是我解决问题的方法: 我在Views/Shared/EditorTemplates中创建了一个UserControl并将其命名为“DateTime”(DateTime.ascx),添加了以下内容:
@model Nullable<System.DateTime>
@if (Model.HasValue)
{
@Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model.Value), new { @class = "date" })
}
else
{
@Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", ""), new { @class = "date" })
}
@{
string name = ViewData.TemplateInfo.HtmlFieldPrefix;
string id = name.Replace(".", "_");
}
<reference path="jquery-1.4.4.js" />
<reference path="jquery-ui.js" />
<script type="text/javascript">
$(document).ready(function () {
$(".date").datepicker
({
dateFormat: 'dd/mm/yy',
showStatus: true,
showWeeks: true,
highlightWeek: true,
numberOfMonths: 1,
showAnim: "scale",
showOptions: {
origin: ["top", "left"]
}
});
});
</script>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd}", Model.HasValue ? Model : DateTime.Today), new { @class = "dp"})%>
我当时的看法是这样的:
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<script type="text/javascript">
$(function () {
$(".dp").datepicker();
});
@EditorFor(model=>model.ReleaseDate)
@Html.ValidationMessageFor(model=>model.ReleaseDate)
我的脚本如下所示:
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<script type="text/javascript">
$(function () {
$(".dp").datepicker();
});
$(函数(){
$(“.dp”).datepicker();
});
这些是我添加的链接(如果您没有):
您可以发布浏览器中呈现的html吗?谢谢,我按照您所说的做了一切,但结果是一样的。什么也没发生