【问题标题】:why it doesnt display Date and time picker in MVC 4为什么它在 MVC 4 中不显示日期和时间选择器
【发布时间】:2016-02-22 12:54:41
【问题描述】:

我正在创建一个 MVC 项目,我对它很陌生。我有一个具有日期时间属性的创建视图。

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

【问题讨论】:

  • 你能告诉我们你的模型类吗?
  • 顺便说一句,我不想​​要一个花哨的选择器,基本会为我完成这项工作
  • 你用什么浏览器来测试这个?

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


【解决方案1】:

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

<input type="date"' 

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

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

【讨论】:

    【解决方案2】:

    @Html.EditorFor 在运行时会创建一个html &lt;textbox /&gt; 标签。

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-02
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2014-11-04
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多