默认模型绑定器不绑定 IEnumerable 中的 Nullable 类型
我有一个控制器操作,其定义如下
public ActionResult ChangeModel( IEnumerable<MyModel> info, long? destinationId)
- 模型:
public class MyModel
{
public string Name; //Gets populated by default binder
public long? SourceId; //remains null though the value is set when invoked
}
'Name' 属性在控制器操作中填充,但 SourceId 属性保持为空。 destinationId 是一个 long? 参数,也会被填充。
在单步执行 MVC(版本 2)源代码时,这是 DefaultModelBinder 引发的异常。
从类型“System.Int32”到类型的参数转换 'System.Nullable`1[[System.Int64,mscorlib,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]]' 失败,因为没有类型转换器可以在这些类型之间进行转换。
如果模型更改为 long 而不是 long?,则默认模型绑定器会设置该值。
public class MyModel
{
public string Name {get;set;}; //Gets populated by default binder
public long SourceId {get;set;}; //No longer long?, so value gets set
}
这是一个已知问题吗?由于 MVC 源代码已经过优化,因此我无法单步执行大部分代码。
更新:发送的请求是使用 Json 的 Http POST,源 Json 类似于 -
{"info":[{"Name":"CL1","SourceId":2}], "destinationId":"1"}
I have a controller action whose definition looks like-
public ActionResult ChangeModel( IEnumerable<MyModel> info, long? destinationId)
And the model:
public class MyModel
{
public string Name; //Gets populated by default binder
public long? SourceId; //remains null though the value is set when invoked
}
The 'Name' property gets populated in the controller action however the SourceId property remains null. The destinationId which is a long? parameter gets populated as well.
While stepping through the MVC (version 2) source code this is the exception thrown by DefaultModelBinder.
The parameter conversion from type 'System.Int32' to type
'System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
failed because no type converter can convert between these types.
If the model is changed to long instead of long?, the default model binder sets the value.
public class MyModel
{
public string Name {get;set;}; //Gets populated by default binder
public long SourceId {get;set;}; //No longer long?, so value gets set
}
Is this a known issue? Since the MVC source code is optimized, I am not able to step through most of the code.
Update: The request being sent is a Http POST using Json with the source JSon resembling -
{"info":[{"Name":"CL1","SourceId":2}], "destinationId":"1"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许为时已晚,但我已经找到了解决方法。您可以在发送数据之前将 SourceId 字段转换为字符串。因此,您的 JSON 数据将类似于
{"info":[{"Name":"CL1","SourceId":"2"}], "destinationId":"1"}
这适用于我的情况(Int32 -> 小数?,ASP NET MVC 3)
Maybe it's too late, but I have found a workaround. You can convert the SourceId field to string before sending data. So your JSON data will look like
{"info":[{"Name":"CL1","SourceId":"2"}], "destinationId":"1"}
This worked in my situation (Int32 -> decimal?, ASP NET MVC 3)
我建议您在视图模型上使用属性而不是字段:
现在以下请求:
很好地填充模型。
I would recommend you to use properties instead of fields on your view model:
Now the following request:
Populates the model fine.
默认模型绑定器将所有
SourceId
值解析为整数。但 .NET 似乎缺少从int
到long?
的默认类型转换器。我要做的是针对这种情况实现类型转换器。
The Default Model Binder is parsing all
SourceId
values as ints. But it seems .NET is missing a default type converter fromint
tolong?
.What I would do is implementing a type converter for that case.