如何将jquery序列化数组传递给webapi2

时间:2015-11-21 08:09:13

标签: jquery ajax asp.net-web-api

如何将此SerializedArray传递给webApi方法
这是我的序列号

[{"name":"txt_id-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"f9bf4d37-c289-4a4f-8ffe-4d425bbedd67"},{"name":"txt_firstname-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"john"},{"name":"txt_lastname-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"doe"},{"name":"txt_age-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"5"},{"name":"txt_id-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"f9ee0ed7-b31d-4b99-932b-51a47fb23ceb"},{"name":"txt_firstname-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"ahmed"},{"name":"txt_lastname-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"zamaroo"},{"name":"txt_age-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"1"},{"name":"txt_id-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"1cd405ca-91e5-4491-8e5c-a64053d6adf6"},{"name":"txt_firstname-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"vahid"},{"name":"txt_lastname-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"khorasani"},{"name":"txt_age-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"1"},{"name":"txt_id-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"9a3d5338-48ef-4ea0-8590-e3397af9e209"},{"name":"txt_firstname-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"Joe"},{"name":"txt_lastname-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"jackson"},{"name":"txt_age-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"1"}]

我的webApi方法

 public void PutCollection( List<Dictionary<string,string>> form)
    {
       \\My Codes Are Here
    }

这是我的Ajax

 $.ajax({
    type: 'PUT',
    url: '/api/Person',
    data: url,
    //contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
        hidePreloader();
        BindTable();
        return callback(data);

    },
    error: function () {
        hidePreloader();

        alert("Error in Method Add Record");
    }
});

现在我在方法PutCollection中的输入为空。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

你快到了,

$.ajax({
type: 'PUT',
url: '/api/Person',
data:[{"name":"txt_id-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"f9bf4d37-c289-4a4f-8ffe-4d425bbedd67"},{"name":"txt_firstname-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"john"},{"name":"txt_lastname-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"doe"},{"name":"txt_age-f9bf4d37-c289-4a4f-8ffe-4d425bbedd67","value":"5"},{"name":"txt_id-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"f9ee0ed7-b31d-4b99-932b-51a47fb23ceb"},{"name":"txt_firstname-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"ahmed"},{"name":"txt_lastname-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"zamaroo"},{"name":"txt_age-f9ee0ed7-b31d-4b99-932b-51a47fb23ceb","value":"1"},{"name":"txt_id-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"1cd405ca-91e5-4491-8e5c-a64053d6adf6"},{"name":"txt_firstname-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"vahid"},{"name":"txt_lastname-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"khorasani"},{"name":"txt_age-1cd405ca-91e5-4491-8e5c-a64053d6adf6","value":"1"},{"name":"txt_id-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"9a3d5338-48ef-4ea0-8590-e3397af9e209"},{"name":"txt_firstname-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"Joe"},{"name":"txt_lastname-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"jackson"},{"name":"txt_age-9a3d5338-48ef-4ea0-8590-e3397af9e209","value":"1"}],
beforeSend: function(xhr){
    xhr.setRequestHeader('Content-Type', 'application/json');
},
dataType: 'json',
success: function (data) {
    hidePreloader();
    BindTable();
    return callback(data);

},
error: function () {
    hidePreloader();

    alert("Error in Method Add Record");
}
});