2015-09-26 135 views
1

該課程通過AJAX帖子進行填充。我該如何檢查一個可爲空的整數數組是否爲空?

public class FilterViewModel 
     { 
      public int?[] size { get; set; } 
      public decimal? Price { get; set; } 
     } 

價格屬性可以很容易地通過

if (Price.HasValue) 
{ 

} 

但對於大小屬性來檢查? 雖然被聲明爲可爲空和內不存在任何數據,這裏是燒出來......

enter image description here

下面是數據發佈

enter image description here

而在RAW

{"man":"2","size":"","color":"","Order":"0","Sorting":"0"} 

該Ajax帖子被執行爲

$.ajax({ 
       url: path, type: "POST", cache: "false", 
       dataType: "json", contentType: "application/json; charset=utf-8", 
       data: JSON.stringify(postData), 
       traditional: true, 
       converters: {'text json': true} 
      }).success(function (responseText) { 
       $('#Grid').replaceWith(responseText); 
      }).error(function (responseText){ 
       swal("Error!", "Ooops", "error"); 
      }); 
+2

僅用於說明目的:'size'是一個可爲空的整數*的數組,您不「將數組聲明爲可空」 - 數組是參考類型。 – kaveman

+0

@kaveman我完全同意,但是我的大腦燒了什麼,是從哪裏產生位置0的空值。 – OrElse

+3

那麼,AJAX的帖子有問題嗎?數組如何生成服務器端? –

回答

2

數組是引用類型,而不管存儲了哪種類型的值以便檢查空值 - size == null

但它看起來像你實際上有數組中的元素 - 你也可以檢查是否所有元素都是「空」,像size.All(x => !x.HasValue)

請注意,以上回答您的問題,但它不太可能有助於解決您可能與請求發送到服務器的方式有關的問題。正如在使用Fiddler來評論確切的請求可能有所幫助。

+0

謝謝你的回答。我會嘗試這一個。我也更新了發佈的數據。 – OrElse