如何通过ajax将对象数组列表发送到控制器

时间:2018-11-09 09:23:26

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

我的javascript中有一个对象,其中有一个对象数组。我想通过ajax调用将其发送到我的控制器。但是我的列表似乎永远不会填充到我的控制器中。 我使我的machineList对象如下:

 var machineList = JSON.stringify({ 'machineList': objects.machines });

该对象的Console.log

{"machineList":[{"Id":1,"Labour":"Hard","EnlistedMachine":"BEXTE","Type":"dz","Identifier":"ddd","IdentifierCode":"ddd"},{"Id":2,"Labour":"Easy","EnlistedMachine":"BEXTEss","Type":"dz","Identifier":null,"IdentifierCode":null}]}

我发送的数据对象如下

 var data = {

        SalesPrice: $("#SalesPrice").val(),
        machineList: machineList

    };

Ajax调用:

   $.ajax({
        url: currenturl + "/MyXmlAction",
        data: data,
        dataType: "json",
        type: "GET",
        contentType: 'application/json; charset=utf-8', //define a contentType of your request
        cache: false,
        success: function (type) {
            // data is your result from controller
            if (type.success) {
                XML = type.json;

            }
        },
        error: function (xhr) {
            alert('error');
        }
    });

我的视图模型如下:

    public class ContractViewModel
    {
  public string SalesPrice { get; set; }
  List<MachineListDto> machineList = new List<MachineListDto>();
     }

我的controllerMethod看起来像:

public ActionResult MyXmlAction(ContractViewModel data)
{
    //Code
    return Json(new { success = true, data }, JsonRequestBehavior.AllowGet);
}

MachineListDto

    public class MachineListDto
    {
        public int Id { get; set; }

        public string EnlistedMachine { get; set; }

        public string Type { get; set; }

        public string Labour { get; set; }

        public string Identifier { get; set; }

        public string IdentifierCode { get; set; }
    }
}

Tetsuya实施更改后的数据对象

{"SalesPrice":"1000","machineList":[{"Id":1,"Labour":"Hard","EnlistedMachine":"BEXTE","Type":"dz","Identifier":"ddd","IdentifierCode":"ddd"},{"Id":2,"Labour":"Easy","EnlistedMachine":"BEXTEss","Type":"dz","Identifier":null,"IdentifierCode":null}]}

我尝试做以下文章中看到的相同操作: Passing ListObject to controller

2 个答案:

答案 0 :(得分:1)

您在代码中遇到3个主要问题:

1)AJAX调用中的contentType设置为application/json; charset=utf-8,这意味着传递的数据必须是JSON字符串,但是您传递的是对象。您需要通过在整个对象定义上放置JSON.stringify()来将两个对象都作为JSON字符串发送:

var data = JSON.stringify({
    SalesPrice: $("#SalesPrice").val(),
    machineList: objects.machines
});

2)List<MachineListDto> machineList = new List<MachineListDto>();的声明定义了一个字段,而不是序列化所需的属性。必须将其声明为属性:

public class ContractViewModel
{
    public string SalesPrice { get; set; }
    List<MachineListDto> machineList { get; set; } // define as property
}

3)AJAX回调的类型设置为GET,这意味着它将作为查询字符串发送,不建议传递集合对象。您需要在AJAX回调中使用type: 'POST',并在控制器操作上设置[HttpPost]属性:

AJAX

$.ajax({
    url: currenturl + "/MyXmlAction",
    data: data,
    dataType: "json",
    type: "POST", // use POST request
    contentType: 'application/json; charset=utf-8', //define a contentType of your request
    cache: false,
    success: function (type) {
        // data is your result from controller
        if (type.success) {
            XML = type.json;

        }
    },
    error: function (xhr) {
        alert('error');
    }
});

控制器操作

[HttpPost]
public ActionResult MyXmlAction(ContractViewModel data)
{
    // do something
    return Json(new { success = true, data }, JsonRequestBehavior.AllowGet);
}

答案 1 :(得分:-1)

var pushSalesprice = [];
var Machinelist_data = [];
pushSalesprice.push($("#SalesPrice").val());
Machinelist_data.push(machineList);
var data = {
  SalesPrice: pushSalesprice ,
  machineList: Machinelist_data 
};