我正在尝试将对象列表(或对象数组)发布到c#Webmethod。我了解如何在方法中作为参数接收并转换为本地对象列表?

for (var i = 0; i < usersInfo.length; i++) {
      user = {
               UserName : usersInfo[i].UserName,
               Email : usersInfo[i].Email,
               Status : status
      };

      users.push(user);
}
var results = "";
$('#lblError').val('');
if (users.length > 0) {
   $.ajax({
          type: 'POST',
          contentType: "application/json; charset=utf-8",
          url: 'UserValidation.aspx/ShowResults',
          data: "{'UsersInfo':'" + JSON.stringify(users) + "'}",
          async: false,
          success: function (data) {
          results = data.d;
          $('#lblError').val(results);

         },
        error: function (xhr, status, error) {
                 var exception = JSON.parse(xhr.responseText);
                 alert(exception.Message);
               }
         });
 }


后面的代码

[WebMethod]
    public static void ShowResults(//Here how receive list object from javascript)
    {
           //convert parameter to List<UsersInfo>
    }

    public partial class UsersInfo
    {
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Status { get; set; }
    }

最佳答案

尝试更换这条线

data: JSON.stringify({ UsersInfo: users}),

10-06 08:12