2014-12-05 135 views
0

我有一个表单,其数据要传递给我的控制器。这是JQuery的电话,我让 -如何将serializeArray作为json传递给MVC控制器

var data = $form.serializeArray(); 

    var options = { 

     contentType: "application/json", 
     url: "/Form/Save", 
     data: JSON.stringify(data), 
     type: "post", 
     datatype: "html", 
     success: function (result, xhr, status) { 
      console.log(result); 
     }, 
     error: function (xhr, status, error) { 
      // redirect to error page 
     } 
    }; 

    $.ajax(options); 

这是怎么我在控制器recieving本 -

public ActionResult Save(string paramJson) 
    { 
     // save paramJson 

     return null; 
    } 

但所有我在保存行动recieving是paramJson = NULL。下面我试过以及 -

数据:JSON.stringify({paramJson:数据})

,但没有奏效。这里应该做什么?

+0

你为什么要回发一个'string'?您可以序列化您的表单并将其发送回模型。 – 2014-12-05 23:56:58

+0

原因是由于jQuery.validate()存在问题,有些字段的'Name'标记与字段名称不同。() – Sam 2014-12-06 12:55:03

+0

jQuery没有问题。如果你的'name'属性不正确是因为你没有正确构造html。这就是你应该修复,而不是试图一些破解。 – 2014-12-06 22:26:58

回答

2

我花了一些暗示,从上面的回答,并能框架,其完美的作品对我的解决方案。我现在收到格式为formname: formvalue而不是name: formname, value: formvalue格式的json。这里是 -

var json = {}; 

    // converting to formname:formvalue format 
    $.each($form.serializeArray(), function (i, field) { 
     json[field.name] = field.value || ''; 
    }); 

    // stringify the parameter 
    var data = { paramJson: JSON.stringify(json) }; 

    var options = { 

     contentType: "application/json", 
     url: "/Form/Save", 
     data: JSON.stringify(data), 
     type: "post", 
     datatype: "html", 
     success: function (result, xhr, status) { 
      console.log(result); 
     }, 
     error: function (xhr, status, error) { 
      // redirect to error page 
     } 
    }; 

    $.ajax(options); 
+1

修复你的视图,你可以使用'form.serialize()'并回发你的模型。这是没有必要的! – 2014-12-07 22:31:56

+0

我的模型中有几个对象字段数组。我尝试序列化,但值没有序列化为数组。还有一个问题,当我将Id域绑定到视图时,它没有得到验证。你能告诉如何解决这些问题吗? – Sam 2014-12-08 03:48:10

+0

没有看到你的模型和视图我不能告诉。但是,如果你总是使用强类型助手(例如'@ Html.TextBoxFor(m => m.SomeProperty)')并使用'for'(而不是'foreach')循环或'EditorTemplates'生成集合,那么你应该没有问题。 'name'属性将始终与您的属性匹配,并在您回传时被绑定。 – 2014-12-08 03:53:23

2

Ajax调用有错误的contentType,它应该像

var data = { paramJson: JSON.stringify($form.serializeArray()) }; 

var options = { 

    contentType: "text/plain", 
    url: "/Form/Save", 
    data: JSON.stringify(data), 
    type: "post", 
    datatype: "html", 
    success: function (result, xhr, status) { 
     console.log(result); 
    }, 
    error: function (xhr, status, error) { 
     // redirect to error page 
    } 
}; 

$.ajax(options); 
0

很好,我也尝试以上所有解决方案,但什么工作对我来说是一种选择“的传统:真正的”在Ajax调用。请遵循以下代码;

var Fixtures = [2,3,4]; var UnitViews = [4,3,6,8];

 $.ajax({ 
     url: '/RHomes/umbraco/surface/Propertysurface/GetPropertiesBL', 
     async: false, 
     type: "GET", 
     data: { 
      iServiceId: serviceid, iLocationID: locationid, 
      iCategoryId: categoryid, iTypeId: propertyTypeid, 
      idevhold: developmentHold, iminbed: minBedRoom, 
      imaxbed: maxBedRom, iminPrice: minPrice, 
      fixtures: Fixtures, imaxPrice: maxPrice, 
      views: UnitViews 
     }, 
     dataType: "html", 
     traditional: true, 
     contentType: "application/json", 
     error: function (data) { 
      alert(data.value); 
     }, 
     success: function (data, textStatus, jqXHR) { 
      //alert(criteria + 'success...' + data); 
      console.log(data); 
      $("#ResultContainer").empty().append(data); 
     } 
    }); 

希望能帮助别人。

相关问题