0

我一直在努力让这篇文章在我的项目中发挥作用。基本上,我将一个 Install 对象列表(一个 install 对象包含一个 Facility 对象列表)传递给我的控制器。我还没有让绑定成功工作。我已经能够获取列表项的计数以反映传递给控制器​​的项数,但是没有在单个对象中设置任何属性。

这是执行帖子的代码:

$("#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

任何帮助将不胜感激 - 在此先感谢!

更新

这是完成此操作的工作代码:

自定义模型绑定器:

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。

4

1 回答 1

1

创建一个 ModelBinder,它将你的 Jsonstring 绑定到一个 List(of InstallInputModel) 并在 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;
        }


    }
}

在 Global.asax 的 Application_Start 中,添加:

ModelBinders.Binders.Add(typeof(List<InstallInputModel>), new JsonBinder());
于 2013-05-14T16:39:07.283 回答