why it doesnt display Date and time picker in MVC 4

五迷三道 提交于 2019-12-25 03:47:17

问题


I am creating an MVC project, I am very new to it. I have Create View which has a datetime propery.

In Html, it just displays a simple textbox for datetime property. How can I make it Date and Time picker? And, here is my HTML code below...

<div class="form-group">
        @Html.LabelFor(model => model.CreatedTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10 datetimepicker">
            @Html.EditorFor(model => model.CreatedTime, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CreatedTime, "", new { @class = "text-danger" })
        </div>
    </div>

and my model class is like below

public partial class AntsoftCrmProjectTransaction
{
    public int Id { get; set; }
    public int ProjectId { get; set; }
    public int MinutesSpent { get; set; }
    public string ProblemDescription { get; set; }
    public bool ProblemIsFixed { get; set; }
    public string ProblemResult { get; set; }
    public string CurrentlyInCharge { get; set; }
    public int SupportId { get; set; }
    public int UserId { get; set; }
    public System.DateTime CreatedTime { get; set; }

回答1:


You will need some type of HTML control for that.

<input type="date"' 

has support in some browsers but it is limited depending on your browser and version and is only supported in HTML5.

Your best solution is to find something like the jquery date and time picker. Use that and add some script to allow the user to pick the date time.




回答2:


The @Html.EditorFor will create a html <textbox /> tag when run.

What you have to do is use a javascript library to convert that EditorFor into a DateTimePicker (you could use the JQueryUI - more details and a working example on msdn, here), something like:

<script type="text/javascript">
    $(function () {
        // This should be the element generated by your EditorFor()  
        $('#CreatedTime').datepicker();
    })
</script>


来源:https://stackoverflow.com/questions/35554285/why-it-doesnt-display-date-and-time-picker-in-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!