2013-05-14 69 views
0

我一直在努力讓這篇文章在我的項目中工作。基本上,我將一個Install對象列表(一個安裝對象包含一個Facility對象列表)傳遞給我的控制器。我還沒有讓綁定成功工作。我已經能夠獲得列表項目的數量以反映傳遞給控制器​​的項目數量,但是沒有任何屬性正在單個對象中設置。在MVC3控制器中綁定複雜json模型的問題

以下是處理後的代碼:

$("#OpenFacilityResults").button().click(function() { 
    var installHolder = { 
    InstallList: [] 
    } 
    var foundInstall = false 
    $('.FullExport').each(function() { 
    //var Install = { InstallRecnum: 0, FacilityList: [Facility, Facility] } 
    //var Facility = { ID: 0, Product: 0 } 
    if ($(this).prop('checked')) { 
     var substr = $(this).parent().find('input:hidden').val().split('|'); 
     var fac = { 
     ID: substr[1], 
     Product: substr[2] 
     } 
     foundInstall = false 
     $.each(installHolder.InstallList, function (index, value) { 
     if (value.InstallRecnum == substr[0]) { 
      foundInstall = true 
      installHolder.InstallList[index].FacilityList.push(fac); 
     } 
     }); 
     if (foundInstall == false) { 
     var i = { 
      InstallRecnum: substr[0], 
      FacilityList: [] 
     } 
     i.FacilityList.push(fac) 
     installHolder.InstallList.push(i) 
     } 
    } 
    }); 
    console.log(JSON.stringify(installHolder)); 
    var dataHolder = JSON.stringify(installHolder) 
    $.ajax({ 
    url: '@Url.Action("ViewFacilityDetails", "CHI")', 
    type: 'POST', 
    dataType: 'json', 
    data: dataHolder, 
    contentType: 'application/json', 
    success: function (data) { 
     // get the result and do some magic with it 
    } 
    }); 
}); 

這裏是JSON的樣子:

{"InstallList":[{"InstallRecnum":"140","FacilityList":[{"ID":"0","Product":"1"}]},{"InstallRecnum":"138","FacilityList":[{"ID":"0","Product":"1"}]}]} 

這裏是控制器動作:

Function ViewFacilityDetails(ByVal InstallList As List(Of InstallInputModel)) As ActionResult 
    Return Json(InstallList) 
End Function 

而我試圖綁定的對象:

<Serializable()> _ 
Public Class InstallInputModel 
    Public InstallRecnum As Integer 
    Public FacilityList As List(Of FacilityInputModel) 
End Class 
<Serializable()> _ 
Public Class FacilityInputModel 
    Public ID As Integer 
    Public Product As Integer 
End Class 

任何幫助將非常感謝 - 提前致謝!

UPDATE

這裏是工作的代碼來實現:

自定義模型綁定:

Public Class JsonBinder 
    Implements IModelBinder 

    Public Function BindModel(controllerContext As System.Web.Mvc.ControllerContext, bindingContext As System.Web.Mvc.ModelBindingContext) As Object Implements System.Web.Mvc.IModelBinder.BindModel 
     If controllerContext Is Nothing Then 
      Throw New ArgumentNullException 
     End If 
     If bindingContext Is Nothing Then 
      Throw New ArgumentNullException 
     End If 

     Dim request = controllerContext.HttpContext.Request 

     request.InputStream.Seek(0, IO.SeekOrigin.Begin) 
     Dim jsonString = New StreamReader(request.InputStream).ReadToEnd 

     If Not jsonString Is Nothing Then 

      Dim deserializer As New System.Web.Script.Serialization.JavaScriptSerializer 
      Dim result = deserializer.Deserialize(Of InstallInputModelHolder)(jsonString) 

      Return result 
     Else 
      Return Nothing 
     End If 

    End Function 
End Class 

* Gloabl.asax.vb除了*

ModelBinders.Binders.Add(GetType(InstallInputModelHolder), New JsonBinder()) 

我很可能會更改JSON,以便它可以傳遞數組,並且不需要額外的容器類來反序列化JSON。

回答

1

創建將您Jsonstring結合(InstallInputModel的)的列表中ModelBinder的,並在Global.asax中

註冊它把我的頭頂部(C#):

public class JsonBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     //return base.BindModel(controllerContext, bindingContext); 
     if (controllerContext == null) 
     { 
      throw new ArgumentNullException("controllerContext"); 
     } 

     if (bindingContext == null) 
     { 
      throw new ArgumentNullException("bindingContext"); 
     } 


     // Get the JSON data that's been posted 
     var request = controllerContext.HttpContext.Request; 

     request.InputStream.Seek(0, SeekOrigin.Begin); 
     var jsonString = new StreamReader(request.InputStream).ReadToEnd(); 


     if (jsonString != null) 
     { 
      var serializer = new JavaScriptSerializer(); 
      var result = serializer.Deserialize(jsonString, typeof(List<InstallInputModel>)); 
      return result; 

     } 
     else 
     { 
      return null; 
     } 


    } 
} 

在Application_Start的Global.asax,加:

ModelBinders.Binders.Add(typeof(List<InstallInputModel>), new JsonBinder()); 
+0

完美!這基本上就是它所需要的 - 當它反序列化但是自定義模型聯編程序是解決方案時,我確實必須添加一個容器來容納安裝對象列表以匹配JSON。非常感謝您的幫助! – Skraps 2013-05-14 21:41:34