简体   繁体   English

ASP.NET MVC日期时间编辑器

[英]ASP.NET MVC datetime editor

I'm learning ASP.NET MVC and I want to create editor for date and time. 我正在学习ASP.NET MVC,我想创建日期和时间编辑器。

Here is the part of my model 这是我模型的一部分

[Display(Name = "Activity Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm}", ApplyFormatInEditMode = true)]
public DateTime activityDate { get; set; }    

and part of my view 和我的观点的一部分

@Html.EditorFor(model => model.activityDate, new { htmlAttributes = new { @class = "dateTimePicker form-control" } })

Now when I am trying to edit this field data is just a string, but if I change model like this: 现在,当我尝试编辑此字段数据时,只是一个字符串,但是如果我更改模型,如下所示:

[Display(Name = "Activity Date")]
[DataType(DataType.Date)]//changed line
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm}", ApplyFormatInEditMode = true)]
public DateTime activityDate { get; set; }

The editor becomes fancy with ability to change values by mouse wheel scrolling and a well designed datePicker, but with out ability to set time. 该编辑器非常喜欢通过鼠标滚轮滚动和设计良好的datePicker来更改值的功能,但无法设置时间。

Can I get both fancy look and UI and ability to change time? 我可以同时获得漂亮的外观和UI以及改变时间的能力吗?

i tried to use this datetime picker for jquery . 我试图将此日期时间选择器用于jquery Here is my html that i get rendered in browser 这是我在浏览器中呈现的HTML

<input class="form-control text-box single-line" data-val="true" data-val-date="The field Activity Date must be a date." data-val-required="The Activity Date field is required." id="datetimepicker" name="activityDate" type="datetime" value="2015-01-14 12:00">

and this is part of my View 这是我观点的一部分

<script>jQuery('#datetimepicker').datetimepicker();</script>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/dateTimePicker")
}
@Styles.Render("~/Content/dateTimePicker1")`

but it slill doesn't work. 但是它没用。

Just change the order, and add document.ready . 只需更改顺序,然后添加document.ready Not: 不:

<script>jQuery('#datetimepicker').datetimepicker();</script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/dateTimePicker")
}
@Styles.Render("~/Content/dateTimePicker1")`

But: 但:

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/dateTimePicker")
}
@Styles.Render("~/Content/dateTimePicker1")`
// type='text/javascript' will allow to work it in IE7
<script type="text/javascript">
   // that will help you to be sure that script will execute just when document is ready
   $(document).ready(function(){
       $('#datetimepicker').datetimepicker();
   })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM