2014-01-29 36 views
0

我剛剛解決了上述問題。我正在向控制器發送JSON對象,但是一旦進入控制器方法,對象的屬性全部爲空。MVC4反序列化控制器中的JSON對象

修復方法是添加{get;組; }到它的類中的所有對象的屬性。房產已經公開,但這還不夠。

誰能告訴我爲什麼這樣嗎?

+1

如果沒有setter,模型綁定器如何設置模型的屬性? –

+0

爲什麼不能只使用傳統的方法 - object.property = blah?該屬性聲明爲公開 –

+0

也許發佈一個正在傳遞給控制器​​的JSON的示例,並且綁定的類將幫助理解問題。 –

回答

0

要使它成爲答案,如果您查看MVC DefaultModelBinder.cs代碼,在綁定複雜元素時,您可以看到modelbinder僅綁定模型的屬性。

internal void BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model) 
    { 
      // need to replace the property filter + model object and create an inner binding context 
      ModelBindingContext newBindingContext = CreateComplexElementalModelBindingContext(controllerContext, bindingContext, model); 
      // validation 
      if (OnModelUpdating(controllerContext, newBindingContext)) 
      { 
       BindProperties(controllerContext, newBindingContext); 
       OnModelUpdated(controllerContext, newBindingContext); 
      } 
    }