默认模型绑定器不绑定 IEnumerable 中的 Nullable 类型

发布于 2024-11-04 18:01:08 字数 1127 浏览 0 评论 0原文

我有一个控制器操作,其定义如下

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 技术交流群。

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

发布评论

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

评论(3

我早已燃尽 2024-11-11 18:01:08

也许为时已晚,但我已经找到了解决方法。您可以在发送数据之前将 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)

静若繁花 2024-11-11 18:01:08

我建议您在视图模型上使用属性而不是字段:

public class MyModel
{
    public string Name { get; set; }
    public long? SourceId { get; set; }
}

现在以下请求:

/somecontroller/changemodel?destinationId=123&info[0].Name=name1&info[0].SourceId=1&info[1].Name=name2&info[1].SourceId=2

很好地填充模型。

I would recommend you to use properties instead of fields on your view model:

public class MyModel
{
    public string Name { get; set; }
    public long? SourceId { get; set; }
}

Now the following request:

/somecontroller/changemodel?destinationId=123&info[0].Name=name1&info[0].SourceId=1&info[1].Name=name2&info[1].SourceId=2

Populates the model fine.

葬花如无物 2024-11-11 18:01:08

默认模型绑定器将所有 SourceId 值解析为整数。但 .NET 似乎缺少从 intlong? 的默认类型转换器。

我要做的是针对这种情况实现类型转换器

The Default Model Binder is parsing all SourceId values as ints. But it seems .NET is missing a default type converter from int to long?.

What I would do is implementing a type converter for that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文