如何通过一类的List到阿贾克斯后的模型称之为MVC? [英] How to pass a List of a class into model of ajax post call MVC?

查看:87
本文介绍了如何通过一类的List到阿贾克斯后的模型称之为MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类象下面这样:

I have a model class like below:

public class clsUser
{
    public string FirstName{ get; set; }
    public String username { get; set; }
    public String password { get; set; }
    public List<ContactNumbers> Cnumbers{ get; set; }
}

现在我想给上述模型Ajax请求。我曾尝试使用下code做相同的:

Now i want to send ajax request with above model. i have tried to do same by using the following code:

  var dd =
   {

   "FirstName": "ABC",
   "username": "abc123",
   "password": "abc@123",
   "Cnumbers[0].Home":"0987654321",
   "Cnumbers[1].Company":"7654321"
   }

    var objInsertUser= JSON.stringify(dd);
    var URLValue = "http://localhost:47083/api/TestAPI/insertUser";
    $.ajax({
        type: "POST",
        url: URLValue,
        data: objInsertUser,
        contentType: "application/json",
        success: function (data) {
            alert(data);
        }

    });

但是,这总是发送Cnumbers为空。我没有得到为什么..

But this always sending "Cnumbers" as null. i am not getting why..

如果我在错误的方式做专家能告诉我正确的方法。

If i am doing in wrong way could experts tell me the correct way..

在此先感谢。

推荐答案

您需要创建适当的JSON。您需要创建一个数组 Cnumbers

You need to create proper JSON. You need to create an array for Cnumbers

var dd = {
    "FirstName": "ABC",
    "username": "abc123",
    "password": "abc@123",
    "Cnumbers": [{
        "Home": "0987654321"
    }, {
        "Company": "7654321"
    }]
}

这篇关于如何通过一类的List到阿贾克斯后的模型称之为MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆