綁定我有一個控制器動作,其定義看起來喜歡 -默認模型綁定不適用於可空類型的IEnumerable
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程序,版本= 2.0.0.0,文化=中性公鑰= 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源代碼已經過優化,因此我無法遍歷大部分代碼。更新
:請求被髮送是HTTP POST使用JSON與源JSON外形酷似 -
{"info":[{"Name":"CL1","SourceId":2}], "destinationId":"1"}
我不知道,如果類型轉換器需要被放入,作爲動作中的其他輸入參數是長? (destinationId),這似乎是填充罰款。 – QED 2011-05-02 17:26:24
@QED:是的,我意識到這個問題 - 這個問題似乎是特定於IEnumerable <>'綁定。不過,這就是我想要的;我假設默認的模型聯編程序顯式地完成了簡單整型類型和它們的可空對象之間的所有轉換,但是對集合並沒有這樣做。是的,這意味着另一種可能的解決方案是覆蓋默認模型綁定器... – rsenna 2011-05-02 17:36:13