asp.net mvc的:默认模型联编问题 [英] asp.net mvc: default model binder problem

查看:83
本文介绍了asp.net mvc的:默认模型联编问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 3视图,其中显示,可以按日期进行筛选的项目列表。

I've got a MVC 3 view which displays a list of items that can be filtered by date.

该过滤器是已jQueryUI的-田间到日期选择器的文本框。

The filter is a textbox which has been jQueryUI-fied into a date picker.

查看:

<%= Html.TextBoxFor(model => model.ReportedDate, new { @class = "datepicker" })%>

脚本:

$(".datepicker").datepicker({
        dateFormat: 'dd/mm/yy',
        changeYear: true,
        changeMonth: true
});

一旦点击一个按钮,我抢文本框的值,并将其发送到我的控制器行动GET请求的查询字符串参数:

Upon a button click, I grab the value of the text box and send it to my controller action as a query string parameter of a GET request:

MyController/Search?reportedDate=30/05/2011

控制器动作:

public ActionResult Search(DateTime? reportedDate)

由此我想到的默认模型绑定到 reportedDate 查询参数转换为可空的DateTime(空在这种情况下再presenting所有日期或无过滤器) 。

From this I expect the default model binder to convert the reportedDate query parameter to a nullable DateTime (null in this context representing all dates or no filter).

然而,这不是这种情况。 reportedDate 总是空。我可以钻入的Request.QueryString 并做转换使用手动 DateTime.TryParse ,这是在我目前的工作,但我不明白为什么它摆在首位失败。

However this is not the case. reportedDate is always null. I can drill into Request.QueryString and do the conversion manually using DateTime.TryParse, which is my current work around, but I don't understand why it's failing in the first place.

有没有在客户端和服务器之间的数据格式没有区别的,还有其他的(此处省略,但在实际的code present)其他数据类型的过滤器参数(字符串和INT),他们得到处理没有问题。

There's no difference in the date format between the client and server and there are other (omitted here but present in the actual code) filter parameters of other data types (string and int) and they get processed without a problem.

任何建议,为什么DateTime的麻烦?

Any suggestions why DateTime is troublesome?

推荐答案

王梦奎是正确的。如果你想使用的日期格式,你需要为你的DateTime自定义模型粘合剂。看看这里:

Veli is right. If you want to use that Date format, you'll need a custom model binder for your DateTime. Take a look here:

如何指定日期格式模型绑定?

这篇关于asp.net mvc的:默认模型联编问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆