我需要在EditorFor字段上使用datepicker.
我的视图代码是:
<div class="editor-label"> @Html.Label("birthDate","birthDate") </div> <div class="editor-field"> @Html.EditorFor(model => model.birthDate,new { @class = "datepicker"}) @Html.ValidationMessageFor(model => model.birthDate) </div>
剧本:
<script type="text/javascript"> $(document).ready( function () { $('.datepicker').datepicker({ changeMonth: true,changeYear: true,minDate: "-99Y",dateFormat: "dd/mm/yyyy" }); });
我不能让它发挥作用.我有以下错误:Microsoft JScript运行时错误:对象不支持此属性或方法’datepicker’.
解决方法
@Html.EditorFor(model => model.birthDate,new { @class = "datepicker"})
不会起作用,因为“EditorFor”不会接受(传递给DOM)一个类.
因此,您需要使用接受Class或更好的“TextBoxFor”,创建一个EditorTemplate来处理任何DateTime字段并在那里添加类. This link详细介绍了如何制作模板.
一旦你的类工作(可以在源代码中验证),其余的只是引用jquery代码.我用
@Styles.Render("~/Content/themes/base/css") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/jqueryui")
并使用您已有的脚本激活它.注意名称’.datepicker’与Class完全相同.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。