简体   繁体   English

如何在 DateTime EditorFor 中设置默认日期的值

[英]How to set value of default date in DateTime EditorFor

I have tried different approach to make a dateTime text box, this is the one that works, but the problem is I cannot set a default date.我尝试了不同的方法来制作 dateTime 文本框,这是可行的方法,但问题是我无法设置默认日期。

Here's what I am working on:这是我正在做的事情:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]

Then I set the date today, DateTime.Now like this model.NotifyDate = DateTime.Now;然后我设置今天的日期, DateTime.Now就像这样model.NotifyDate = DateTime.Now; 在此处输入图像描述

It doesn't set the date.它没有设置日期。 However, if I remove [DataType(DataType.Date)] from the model I will get:但是,如果我从 model 中删除[DataType(DataType.Date)]我会得到: 在此处输入图像描述

I will get the date, but the calendar is gone.我会得到日期,但日历不见了。 What's the problem?有什么问题? Or am I using the datepicker wrong?还是我使用datepicker错误?

Here's my view:这是我的看法:

@Html.EditorFor(m => m.NotifyDate, new { htmlAttributes = new { @class = "form-control input-sm" } })

You were on the right track!你在正确的轨道上! Defaults such as this should be set in the ViewModel像这样的默认值应该在ViewModel中设置
(as it appears you're doing w/ model.NotifyDate = DateTime.Now; ) (看起来你正在使用model.NotifyDate = DateTime.Now;

The problem here appears to be that the browser is expecting the value for the generated html input element to be formatted differently -- namely, yyyy-MM-dd vs yyyy/MM/dd .这里的问题似乎是浏览器期望生成的 html input元素的值格式不同——即yyyy-MM-ddyyyy/MM/dd
(note the use of - vs / ) (注意使用- vs /

In order for the browser to correctly display the date, the value must be formatted as 2019-09-23 .为了让浏览器正确显示日期,该值的格式必须为2019-09-23

ex:前任:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

Here is a great answer to a similar question that should shed some more light on what's going on as well.是对类似问题的一个很好的答案,应该对正在发生的事情有更多的了解。

Since Browser is expecting the value for the generated HTML input, you either need to provide value with client-side javascript(or clientside datepicker) or bind the value to Model.由于浏览器期望生成的 HTML 输入的值,您需要使用客户端 javascript(或客户端 datepicker)提供值或将值绑定到 Model。 You can bind the value to model and pass to view from the controller as follows.您可以将值绑定到 model 并从 controller 传递到视图,如下所示。

public async Task<ActionResult> Create()
    {
         var dateInfo = new DateInfo()
            {
                StartDate = DateTime.Now
            };
            return View(dateInfo);
    }

Or You can assign value from client side.或者您可以从客户端分配价值。 jQuiery Ui is simple and easy to implement. jQuiery Ui 简单易实现。 You can Visit .您可以访问.

You should try this datepicker in js.你应该在 js 中尝试这个 datepicker。

$('.date').datepicker({
            format: "dd.mm.yyyy",
            weekStart: 1,
            clearBtn: true,
            todayBtn: "linked",
            language: "tr",
            startDate: new Date(),
            autoclose: true
        });

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

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