【问题标题】:How to pass a custom class with JSON to Controller如何将带有 JSON 的自定义类传递给控制器
【发布时间】:2016-09-25 00:01:46
【问题描述】:

我在 JS 中有这两个类:

class Puerta {
    constructor(nro, tipo) {
        this.nroPuerta = nro;
        this.tipoPuerta = tipo;
    }
}

class Controladora {
    constructor(ip, segundos, puertas) {
        this.ipControladora = ip;
        this.apuertaSegundosControladora = segundos;
        this.listaPuertas = puertas;
    }
}

我有这段代码,它正确传递了“Controladora”,但在“Controladora”类中没有“Puertas”数组的任何元素。

$(document).ready(function (){
    var puertas = [];
    for(var i = 1;i<=3;i++){
        puertas.push(new Puerta(i,"String " + i);
    }
    var controladora = new Controladora("192.168.1.1", 30, puertas);
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Controladora/Prueba',
        data: JSON.stringify({ 'Controladora': controladora }),
        success: function () {
        },
        failure: function (response) {
        }
    });
});

1

在控制器中,我得到“Controladora”,但 listaPuertas 为空。

我做错了什么?缺少什么?

编辑

这是 C# 中的类:

public class Puerta
{
    public int nroPuerta{ get; set; }
    public string tipoPuerta { get; set; }
}

public class Controladora
{
    public string ipControladora { get; set; }
    public int apuertaSegundosControladora { get; set; }
    public List<Puerta> listaPuertas { get; set; }
}

【问题讨论】:

  • 在问题中显示你的控制器方法(不是图片链接)和你绑定的模型
  • Controladora 的构造函数只接受两个参数,但您传递了三个参数,并使用 this.listaPuertas = [] 初始化空数组
  • 我怀疑您序列化为 JSON 的方式。我认为它需要是这样的:data: JSON.stringify(controladora),。因为你的javascript类需要成功序列化到c#实例。
  • 我试过了,没用,每个属性都是空的。这就是我这样做的原因。

标签: c# json ajax asp.net-mvc post


【解决方案1】:

您当前的 js 代码正在发送如下结构的 ajax 数据

{
    "Controladora": {
        "ipControladora": "",
        "listaPuertas": [{  "nroPuerta":  1 },
                         {   "nroPuerta": 2 },
                         {   "nroPuerta": 3 }]
    }
}

假设您的操作方法接受Controladora 类类型的参数

[HttpPost]
public ActionResult Prueba(Controladora model)
{
    // to do : return something,
}

要使模型绑定起作用,您应该发送类似这样的内容

为此,您只需发送与您的 Controldaora 类的结构相匹配的数据。无需在您的 json 负载中指定参数名称。

{
    "ipControladora": "",
    "listaPuertas": [{      "nroPuerta": 1      },
                     {      "nroPuerta": 1      }, 
                     {      "nroPuerta": 1      }]
}

下面的代码应该可以做到。

var puertas = [];
for (var i = 1; i <= 3; i++) {
    puertas.push({ nroPuerta: 1 });
}
var d = { ipControladora: "192.168.1.1",
                     apuertaSegundosControladora :30,
                     listaPuertas: puertas };


$.ajax({
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '@Url.Action("Prueba","Controladora")',
    data: JSON.stringify(d),
    success: function () {
    },
    failure: function (response) {
    }
});

我用 js 对象替换了 js 类。但是,如果想使用 ECMA 2015 javascript 类,请使用您问题中的相同代码。它应该可以工作。

var puertas = [];
for(var i = 1;i<=3;i++){
    puertas.push(new Puerta(i,"Stringy " + i));
}
var d = new Controladora("192.168.1.1", 30, puertas);
$.ajax({
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '@Url.Action("Prueba","Controladora")',
    data: JSON.stringify(d),
    success: function () {
    },
    failure: function (response) {
    }
});

【讨论】:

  • 是的,它对我有用。但是,如果我想拥有一个带有“Puertas”数组的 js 类“Controladora”并使用 JSON 传递,那么该类将传递给控制器​​?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 2014-09-09
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多