2

I am trying to post List of object (or array of object) to c# Webmethod. I am understanding how to receive as parameter in the method and convert into local List of object?.

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);
               }
         });
 }

Code behind

[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; }
    }
James123
  • 11,184
  • 66
  • 189
  • 343

2 Answers2

0

Try to replace this line

data: JSON.stringify({ UsersInfo: users}),
Dot Freelancer
  • 4,083
  • 3
  • 28
  • 50
  • What is the parameter in public static void ShowResults(...)? – James123 Sep 29 '14 at 21:27
  • What is the use of `JSON.stringify` and double-serialization? Would `data: { UsersInfo: users}` be not enough? – L.B Sep 29 '14 at 21:29
  • Can I use like this ShowResults(List usersInfo)? – James123 Sep 29 '14 at 21:41
  • @James123 I am sorry, but your question was about Front-end.. I don't have enough experience to answer your "back-end" question. But you should be able to read the data from the posted form. – Dot Freelancer Sep 29 '14 at 21:48
  • @L.B jQuery will not convert it to JSON automatically. it will send it like string. – Dot Freelancer Sep 29 '14 at 21:49
  • 1
    @dotfreelancer But my suggestions will also do this. Your code will convert the object to json string and then this string will be converted to another json string(second one is done automatically by $.ajax). So instead of `{"a":"test"}` your code will post `"{\"a\":\"test\"}"` which would require another unnecessary deserialization on server side. – L.B Sep 29 '14 at 21:54
  • @L.B jQuery will attempt to URLEncode your data. see this well up-voted answer here from Dave Ward http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – Dot Freelancer Sep 29 '14 at 22:02
  • @dotfreelancer the correct way would be to download the fiddler and see what is going on under the hood. There isn't any magic. just serialization & deserialization regardless of the technology being used. So I am sure what I am talking about. – L.B Sep 29 '14 at 22:04
  • @dotfreelancer if OP would use it the same way as you suggest then it should declare a string as parameter and deserialize it (*to List – L.B Sep 29 '14 at 22:09
  • @dotfreelancer BTW: `jQuery will attempt to URLEncode your data` is not correct. it depends on the `Content-Type` or another appropriate header. – I4V Sep 29 '14 at 22:14
0

James you are the right track; you need to define the correct type for the ShowResults parameter so that the binding will work and bind the incoming json to your UsersInfo class.

Your UsersInfo class appears to be a simple POCO so should bind without any custom binding logic :

[WebMethod]
public static void ShowResults(List<UsersInfo> UsersInfo)
{
       //No need to convert
}
Steve O
  • 228
  • 1
  • 9