zoukankan      html  css  js  c++  java
  • 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"/>
    }
  • 相关阅读:
    ASP.NET MVC HandleError异常过滤器过滤器用法
    ASP.NET MVC 4 过滤器(Authorize)
    ASP.NET MVC 4 过滤器(Authorize)
    蜜蜂路线
    1220 数字三角形
    Ⅳ.Catalan数
    10:单词排序
    09:明明的随机数
    07:合影效果
    08:病人排队
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779882.html
Copyright © 2011-2022 走看看