asp.net-mvc - 没有模型绑定DateTime字段时如何使用JQueryUI Datepicker?
问题描述
我已经在另一个视图上使用了这个日期选择器,一个接收模型的 Create() 视图,然后我可以使用以下方法显示日期选择器:
<link rel="stylesheet"href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
...
@Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", placeholder = "OrderDate", @readonly = "true" } })
...
@section Scripts
{
@Scripts.Render("~/bundles/jqueryui")
<script type="text/javascript">
jQuery.validator.methods["date"] = function (value, element) { return true; }
$(document).ready(function () {
$("#OrderDate").val("");
$('input[type=datetime]').datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: "-1:+2",
onClose: function (dateText, inst) {
$("#cmdEnter").focus();
}
});
});
</script>
}
现在的问题是我需要在视图中使用日期选择器作为顶部的搜索字段之一,我正在尝试按范围实现日期搜索。这个视图是一个索引,这意味着它不接收模型它接收模型集合(在本例中为 PageList):
@model PagedList.IPagedList<EntregaMedicamentos.Models.PedidoDisprofarmaViewModel>
所以,在这种情况下,我不能使用 model => model.OrderDate,然后我尝试使用 Viewbag 在那里传递那个日期,比如..
@Html.TextBox("searchDateFrom", ViewBag.currentFilter2 as DateTime?, new { @class = "form-control", placeholder = "Desde fecha", @readonly = "true" })
所以我从 EditorFor 更改为 TextBox 并尝试使用 Editor 但日期选择器仍然不想在点击时弹出,有什么想法吗?
这是一个尝试过的,仍然没有弹出窗口:
@Html.TextBox("searchDateFrom", ViewBag.currentFilter2 as DateTime?, new { @class = "form-control", placeholder = "Desde fecha", @readonly = "true" })
....
@section scripts
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
jQuery.validator.methods["date"] = function (value, element) { return true; }
$(document).ready(function () {
$("#searchDateFrom").val("");
$('input[type=datetime]').datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: "-1:+2",
onClose: function (dateText, inst) {
$("#cmdSearch").focus();
}
});
});
</script>
...
谢谢!!
解决方案
您可以使用
<input type="datetime" name="ordredate">
因为@Html.TextBox() 没有生成文本框类型“日期时间”。
推荐阅读
- python - 如何在两个特定单词之间拆分句子以便稍后放入变量?
- python-3.x - 使用 args 从 dict 调用函数
- activemq - 将所有消息从一个队列发送到另一个队列
- azure - 使用 Azurite 又名 Azure 本地存储创建 Azure 存储容器:名称验证错误
- html - 窗口调整大小时的动态剑道网格高度?
- file - 根据 Puppet 中文件的存在应用不同的配置
- gradle - 来自 Git 存储库的 Gradle 直接文件参考
- python - 在 excel 中搜索一行以使用 Python 从关联行中接收数据
- c# - 为什么我看到.Cast 之间的差异
() 和 .Select(a => (int)a)? - python - 从上游客户端 172.17.0.1 读取响应标头时上游超时