通过ajax jquery传递类类型参数

时间:2017-08-17 08:34:12

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

我有以下结构的课程

public class MyType
    {
        [Key]
        [Required]
        public string name { get; set; }
        [Key]
        [Required]
        public string type{ get; set; }

    }


Public Class A
{
string Id{get;set;}
public List<MyType> aList{get;set;}
}

我的ajax呼叫如下:

var listitem=[
{name:"a",type:1},
{name:"b",type:2},
{name:"c",type:3}
]

     $.ajax({
                url: '@Url.Action("ActionMethod", "Controller")',
                type: 'post',
                async: false,
                data: {
                    Id: 1,                    
                    aList :listitem

                },
                success: function (resp) {

                },
                error: function (resp) {
                }
            });

我的行动方法如下:

 [HttpPost]
            public ActionResult ActionMethod(A adata)
            {
                return some action;

            }

我在ActionMethod中获得Id的值,但列表项是空白的?我怎么能通过名单?

1 个答案:

答案 0 :(得分:3)

试试这样:

var adata = {
    Id: 1,
    aList: [
        { name: "a", type: 1 },
        { name: "b", type: 2 },
        { name: "c", type: 3 }
    ]
}

$.ajax({
    url: '@Url.Action("ActionMethod", "Controller")',
    type: 'post',
    data: JSON.stringify(adata),
    contentType: "application/json; charset=utf-8",
    success: function (resp) {

    },
    error: function (resp) {
    }
});