我有一个控制器动作,其定义看起来像 -
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
}
在控制器操作中填充了“名称”属性,但SourceId属性仍然为空。哪个destinationId是长的?参数也被填充。
在单步执行 MVC(版本 2)源代码时,这是 DefaultModelBinder 引发的异常。
从类型 'System.Int32' 到类型 'System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, 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"}