通过ajax调用web方法发布对象数组返回null

时间:2017-04-12 10:32:31

标签: javascript ajax asp.net-mvc c#-3.0

尝试将对象数组发布到post方法但是为null

data="{"ParamObjArr":[{"RegistrationId":"1033","LanguageCode":"en"},{"RegistrationId":"1033","LanguageCode":"en"}]}"

客户方:

$.ajax({
        url: '@Url.Action("PostNewUserEducation", "Home")',
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: data,
        success: function (data) {
            //
        },
        error: function (x, y, z) {
          //
        }
    });

代码背后:

 public async Task<JsonResult> PostNewUserEducation(Dictionary<string,string> ParamObjArr)
    {
            string uriAction = "http://2525/api/NewUser";
            HttpResponseMessage responseMessage = await client.PostAsJsonAsync(uriAction, ParamObjArr);
    }

在web方法中,lstUserEducation为null,如何在字典中接收对象数组

1 个答案:

答案 0 :(得分:0)

最好创建Class,它具有另一个

列表类型的属性

创建2个类,如下所示:

public class ParamObjArr
    {
        public string RegistrationId { get; set; }
        public string LanguageCode { get; set; }
    }

    public class RootObject
    {
        public List<ParamObjArr> ParamObjArr { get; set; }
    }

然后接受你的行动。

public async Task<JsonResult> PostNewUserEducation(RootObject getData)
        {
            string uriAction = "http://2525/api/NewUser";
             HttpResponseMessage responseMessage = await client.PostAsJsonAsync(uriAction, getData);
        }