【问题标题】:List not passed properly via ajax to Controller Action in MVC 4列表未通过 ajax 正确传递到 MVC 4 中的控制器操作
【发布时间】:2014-08-22 19:35:24
【问题描述】:

我通过 ajax 将 List(data) 和 ID(id) 传递给 MVC Action(EditDesignTemplate)。

我在EditDesignTemplate Action 中将参数data 列表的计数设为0,而我可以看到参数id 已从ajax 中正确捕获了该值。

但是如果我提醒它,我可以获取存储在 jquery 中数据变量中的值,因此显然该列表没有作为空参数传递。在下面给出的错误 url 中也可以看到参数。

jQuery:

            var $trows = $('#ContentPlaceHolder1_FrmView_rptcreation_grdtstrpt').find('tr:has(td)');
            var data = [];
            var report_kid = $('#report_kid').val();
            for (var i = 0; i < $trows.length; i++)
            {
                var $tds = $($trows).eq(i).children('td');
                data.push({ 'id': $tds.eq(0).text(), 'text': $tds.eq(1).text() });
                //alert($tds.eq(1).text());
            }

            $.ajax({
                url: "http://localhost:3916/FormsCreation/EditDesignTemplate/",
                type: 'GET',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: {data:JSON.stringify(data), id:escape(report_kid)},
                success: function (result) {
                }
                });

控制器中的操作

public ActionResult EditDesignTemplate(List<data> data, string id)
    {
        var model = new DashBoardModel();
        model.FormDetails = new FormsCreationModel();
        int i=0;
        foreach (var dt in data)
        {
            model.FormDetails.formfields[i].field_id = dt.id;
            model.FormDetails.formfields[i].field_name = dt.text;
            model.FormDetails.formfields[i].field_type = "Field";
            i++;
        }

        model.Controls = new List<ControlsModel>();
        model.Controls = formRepository.GetDesignTempsDet(id);
        model.DesignTemplateId = id;
        return View(model);
    }

班级

public class data
{
    public string id { get; set; }
    public string text { get; set; }
}

收到空列表后的错误消息

XMLHttpRequest cannot load http://localhost:3916/FormsCreation/EditDesignTemplate/?data=%5B%7B%22id%22…22id%22%3A%220000164%22%2C%22text%22%3A%22Sr.+IRON%22%7D%5D&amp;id=0006PL%255C. Invalid HTTP status code 500

问题:

我做错了吗?我不应该通过GET 方法传递列表吗?

当我尝试将其更改为 POST 操作时,我想我什至没有针对该操作。得到了 404 响应..

一些类似的问题

Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

MVC Send list through AJAX

PS:我正在尝试通过 ajax 从 ASP.NET Webform 访问此 MVC 操作。我在我的解决方案中运行双重项目。

【问题讨论】:

    标签: ajax json asp.net-mvc-4


    【解决方案1】:

    试试这个:

    var d=JSON.stringify({ 'data': data,'id':escape(report_kid)});
    $.ajax({
                    url: "http://localhost:3916/FormsCreation/EditDesignTemplate/",
                    type: 'POST',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: d,
                    success: function (result) {
                    }
                    });
    

    HTTPPOST 添加到Action 方法。

    [HttpPost]
    public ActionResult EditDesignTemplate(List<data> data, string id)
        {
            var model = new DashBoardModel();
            model.FormDetails = new FormsCreationModel();
            int i=0;
            foreach (var dt in data)
            {
                model.FormDetails.formfields[i].field_id = dt.id;
                model.FormDetails.formfields[i].field_name = dt.text;
                model.FormDetails.formfields[i].field_type = "Field";
                i++;
            }
    
            model.Controls = new List<ControlsModel>();
            model.Controls = formRepository.GetDesignTempsDet(id);
            model.DesignTemplateId = id;
            return View(model);
        }
    

    【讨论】:

    • 使用 HTTP POST 而不是 GET,我已经更新了我的解决方案。检查一次
    • 你在 ajax 调用中也设置了 type: 'POST' 吗?
    • 是的,我做到了。与您发布的内容类似。
    【解决方案2】:

    试试这个:

    var jsonObj = [];
                 for (var i = 1; i < myTableArray.length; i++) {
                     var item = {};
                     item["Name"] = myTableArray[i][0];
                     item["Value"] = myTableArray[i][1];
    
                     jsonObj.push(item)
                 }
    
    $.ajax({
                     type: "POST",
                     contentType: "application/json; charset=utf-8",
                     url: "webmethod.aspx/dataarraypass",
                     data: "{html:" + JSON.stringify(jsonObj) + "}",
                     dataType: "json",
                     success: function (data) 
    

    使用这个你可以传递多个对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2013-12-15
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      相关资源
      最近更新 更多