【发布时间】:2011-08-16 23:49:36
【问题描述】:
我有一个控制器动作,它的定义看起来像-
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"}
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-2 model-binding defaultmodelbinder