为什么它不在MVC 4中显示日期和时间选择器

时间:2016-02-22 12:54:41

标签: c# asp.net-mvc datetime datepicker

我正在创建一个MVC项目,我对它很新。我有Create View,它有一个datetime属性。

在Html中,它只显示datetime属性的简单文本框。如何使其成为日期和时间选择器?而且,这是我的HTML代码......

<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>

我的模型类如下

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; }

2 个答案:

答案 0 :(得分:1)

@Html.EditorFor会在运行时创建html <textbox />标记。

您需要做的是使用javascript库将EditorFor转换为DateTimePicker(您可以使用JQueryUI - 更多详细信息和msdn上的工作示例,{{ 3}}),类似于:

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

答案 1 :(得分:1)

您需要某种类型的HTML控件。

<input type="date"' 

在某些浏览器中有支持但受限于您的浏览器和版本,并且仅在HTML5中受支持。

您最好的解决方案是查找jquery date and time picker之类的内容。使用它并添加一些脚本以允许用户选择日期时间。

相关问题