这是一个日期时间编辑器,类似于 NerdDinner 的/Views/Shared/EditorTemplates/DateTime.ascx:
%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model))%>
<script type="text/javascript">
$(function () {
$("#date").datepicker({ showOn: 'focus' });
});
</script>
由于这是一个可能会在一个页面上多次使用的模板,如果我要将日期选择器附加到每个文本框,它们每个都需要一个唯一的 ID,并且我需要能够在<脚本>.
执行此操作的最佳方法是什么?
最佳答案
我不是 MVC 程序员,但你肯定可以只使用类而不是 ID 吗?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
<%: Html.TextBox("", String.Format("{0:yyyy-MM-dd HH:mm}", Model, new { @class = "datepicker" }))%>
<script type="text/javascript">
$(function () {
$(".datepicker").datepicker({ showOn: 'focus' });
});
</script>
关于javascript - 在 jquery UI 扩展中使用 Html.EditorFor 生成的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3297371/