ajax 提交数组 泛型集合(二)

最近在项目中,使用 mvc架构,model层使用code first

碰见一个问题,前台json传递数据给后台action的复杂对象,发现复杂对象中的list范型集合并没有获取到数据。

研究半天,终于发现问题所在,现在贴一下解决方案

 

action代码

 

[HttpPost]
        public ActionResult Get(A data)
        {
            return Content("123");
        }


class代码

 

 

public class A
    {
        public string a { get;set;}
        public string b { get; set; }
        public List<B> t { get; set; }
    }

    public class B
    {
        public string c { get; set; }
    }

 

 

AJAX提交方式:

 

 

d = { a: "1", b: "2", t: [{ c: "ccc" }, { c: "c1x1" }] }
        $('#Test').click(function () {
            $.ajax({
                type: "post",
                url: "Get",
                dataType: "json",
                contentType: "application/json;",//这句话很重要
                data: JSON.stringify(d),
                success: function (data) {
                    alert(data);

                }
            });
        })

 

 

 

 

 

前台Submit按钮提交方式:

 

@using (Html.BeginForm("Get", "Test", FormMethod.Post))
{
    @Html.TextBoxFor(item => Model.a)
    @Html.TextBoxFor(item => Model.b)<br>

    int i=0;
    foreach (var item in Model.t)
    {
        @Html.TextBoxFor(a => item.c, new { Name="t["+i+"].c"})//这里需要给Name赋值并且注明下标i
        i++;
    }
    
    <input type="submit" value="Submit"/>
}

 

 

 

 

 

 

posted @ 2016-07-15 15:24  正怒月神  阅读(164)  评论(0编辑  收藏  举报