Telerik MVC DatePicker 上的 onChange 事件行为非常奇怪

发布于 2024-11-19 11:07:31 字数 1064 浏览 5 评论 0原文

对于开始的问题,我的代码(我将尝试仅包含相关部分来开始),从我的脚本开始:

function RaceDate_onChange() {
    var pickedDate = $(this).data('tDatePicker').value();
    var month = pickedDate.getMonth() + 1;
    $.get("/RaceCard/Details?year=" + pickedDate.getFullYear() + "&month=" + month + "&day=" + pickedDate.getDate());
}

然后是我的标记:

@Html.Telerik().DatePickerFor(model => model.RaceDate).ClientEvents(events => events.OnChange("RaceDate_onChange"))

最后是一些接收操作:

[HttpGet]
public ActionResult Details(int year, int month, int day)
{
    var viewModel = new RaceCardModel {Metadata = DetailModelMetadata.Display, RaceDate = new DateTime(year, month, day)};

我正在尝试选择一个新的触发 GET 的日期,刷新页面而不提交表单。这工作正常,除了这个问题:

在对 Details 操作的 GET 请求中,day 值始终比 DatePicker 晚一天。例如,当呈现视图时,第一个值是从视图模型属性设置的,例如 3。然后我单击 14 并在操作方法中命中断点。 day 值为 3。当我点击 29 并命中断点时,day 值为 14。

除了问出了什么问题之外,我还会冒昧地问一下如果有更好的方法那就不再复杂了。我是个相当新手,宁愿交付需要修改的工作代码,也不愿陷入切线和细节之中。

To the question started, my code (I'll try to only include relevant portions to start), starting with my script:

function RaceDate_onChange() {
    var pickedDate = $(this).data('tDatePicker').value();
    var month = pickedDate.getMonth() + 1;
    $.get("/RaceCard/Details?year=" + pickedDate.getFullYear() + "&month=" + month + "&day=" + pickedDate.getDate());
}

Then my markup:

@Html.Telerik().DatePickerFor(model => model.RaceDate).ClientEvents(events => events.OnChange("RaceDate_onChange"))

And finally a bit of the receiving action:

[HttpGet]
public ActionResult Details(int year, int month, int day)
{
    var viewModel = new RaceCardModel {Metadata = DetailModelMetadata.Display, RaceDate = new DateTime(year, month, day)};

I'm trying to get the selection of a new date to trigger a GET, to refresh the page without submitting a form. This works fine, except for this problem:

In GET requests to the Details action, the day value is always one day behind the DatePicker. E.g. The first value is set from a view model property, when the view is rendered, say 3. I then click on 14 and hit my breakpoint in the action method. The day value is 3. When I click on 29 and hit the breakpoint, the day value is 14.

Besides asking what is wrong, I'll take a liberty and ask if there is a better way that is no more complicated. I am fairly novice and would rather deliver working code that needs revision than get bogged down in tangents and details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-11-26 11:07:31

请尝试使用 e.value,如客户端事件示例中所示。您可能使用的是旧版本,其中 value() 方法在 OnChange 事件期间返回先前的值。

更新:

“e.value”表示 OnChange 参数的值字段:

function onChange(e) {
   var date = e.value; // instead of datePicker.value()
}

Try using e.value instead as shown in the client-side events example. You are probably using an older version where the value() method returned the previous value during the OnChange event.

UPDATE:

"e.value" means the value field of the OnChange arguments:

function onChange(e) {
   var date = e.value; // instead of datePicker.value()
}
々眼睛长脚气 2024-11-26 11:07:31

就您得到的 1 个月差异而言,这是正常的,这就是 getMonth() 方法在 Date 实例上的 JavaScript 中工作:

getMonth 返回的值是
0 到 11 之间的整数。0
对应一月,1 到二月,
等等。

所以添加+1是应对这种情况的正确方法,就像你所做的那样。

关于 AJAX 调用的一点评论:永远不要对 url 进行硬编码。处理 url 时始终使用 url 助手:

var year = pickedDate.getFullYear();
var month = pickedDate.getMonth() + 1;
var day = pickedDate.getDate();
var url = '@Url.Action("Details", "RaceCard")';
$.get(url, { year: year, month: month, day: day }, function(result) {
    // process the results of the AJAX call
});

As far as the 1 month difference you are getting, that's normal, and it is how the getMonth() method works in javascript on a Date instance:

The value returned by getMonth is an
integer between 0 and 11. 0
corresponds to January, 1 to February,
and so on.

So adding +1 is the correct way to cope with the situation, exactly as you did.

Just a little remark about your AJAX call: never hardcode urls. Always use url helpers when dealing with urls:

var year = pickedDate.getFullYear();
var month = pickedDate.getMonth() + 1;
var day = pickedDate.getDate();
var url = '@Url.Action("Details", "RaceCard")';
$.get(url, { year: year, month: month, day: day }, function(result) {
    // process the results of the AJAX call
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文