无法在控制器上反序列化json对象

时间:2014-06-11 20:08:54

标签: c# jquery asp.net-mvc-4

我有一个Json

var jdondata=  [
        {
            "address": "address1",
            "email": "a@ab.com",
            "name": "sharad",
            "phone": "222222222"
        },
        {
            "address": "address2",
            "email": "b@gamil.com",
            "name": "hemant",
            "phone": "344324554"
        },
        {
            "address": "address2",
            "email": "c@ss.com",
            "name": "Madhur",
            "phone": "222222"
        }
    ]

和各自的班级

  public class addresslists
    {
        public string address { get; set; }
        public string email { get; set; }
        public string name { get; set; }
        public string phone { get; set; }
    }

我从jquery传递数据,如

 var url = "/ContactGrabber/SaveImportedContacts";
    var postData = { "listGmail": data, "service": "gmail" };
     $.ajax({
                type: "POST",
                url: url,
                data: postData ,
                success: function (response) {
                    if (callback) {
                        callback(response);
                    }
                    // openNewWindow(response);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('error = ' + xhr.responseText);
                }
            });

我的控制器方法是

[HttpPost]
public JsonResult SaveImportedContacts(List<addresslists> listGmail, string service)
{
     return null;
}

现在在listGmail参数中,我得到三个数组元素,但都有空值。服务变量正在正确填充。

请在我错的地方帮助我。

3 个答案:

答案 0 :(得分:2)

您需要使用属性而非字段:

public class addresslists
{
    public string address { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string phone { get; set; }
}

有很多问题。首先,您应始终stringify()您的对象,因为jQuery可能无法正确地将Javascript对象作为表单传递回来。这样做时,需要设置contentType:

$.ajax({
  type: "POST",
  url: url,
  data: JSON.stringify(postData),
  contentType: "application/json; charset=utf-8",
  success: function (response) {
    console.log('success');         
    console.log(response);          
  },
  error: function (xhr, ajaxOptions, thrownError) {
    console.log('error');           
    console.log(xhr.responseText);
  } 
});

我总是建议将您的回复包装到ViewModel中:

public class SaveContactsViewModel
{
  public List<Detail> Details { get; set; }
  public string Service { get; set; }
}

public class Detail
{
  public string Address { get; set; }
  public string Email { get; set; }
  public string Name { get; set; }
  public string Phone { get; set; }
}

然后您的控制器方法应如下所示:

[HttpPost]
public JsonResult SaveImportedContacts(SaveContactsViewModel model)
{
  return null;
}

我花了一些时间用this up in a DotNetFiddle Example将结果包装在console.log(开发人员工具日志)中。我必须离开,但我会在3-4小时内回复。

答案 1 :(得分:1)

您的Json类应如下所示: -

    public class Address
    {
        public string address { get; set; }
        public string email { get; set; }
        public string name { get; set; }
        public string phone { get; set; }
    }

答案 2 :(得分:0)

尝试使用数组而不是List&lt;&gt;

public JsonResult SaveImportedContacts(addresslists[] listGmail, string service)