Telerik MVC DatePicker 上的 onChange 事件行为非常奇怪
对于开始的问题,我的代码(我将尝试仅包含相关部分来开始),从我的脚本开始:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用 e.value,如客户端事件示例中所示。您可能使用的是旧版本,其中 value() 方法在 OnChange 事件期间返回先前的值。
更新:
“e.value”表示 OnChange 参数的值字段:
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:
就您得到的 1 个月差异而言,这是正常的,这就是
getMonth()
方法在Date
实例上的 JavaScript 中工作:所以添加+1是应对这种情况的正确方法,就像你所做的那样。
关于 AJAX 调用的一点评论:永远不要对 url 进行硬编码。处理 url 时始终使用 url 助手:
As far as the 1 month difference you are getting, that's normal, and it is how the
getMonth()
method works in javascript on aDate
instance: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: