0

我正在尝试将 javascript 对象传递给 VB.NET WebApi 2 中的 POST 方法,但问题是传递的对象如下

在此处输入图像描述

WebApi 将所有值接收为 0,即使 negozio 设置为 5,每个值也会发生这种情况,它仍然会收到 0,在这里您可以看到 cassa 值是 0 和 0,而在传递的对象中它们是 1 和 5

在此处输入图像描述

问题可能与模型有关吗?如果是这样,我该如何修复它以获得正确的值?这是我在 VB.NET 中的模型

Public Class Cfg

    Public Property negozio As List(Of NEGOZI)
    Public Property cassa As List(Of CASSE)
    Public Property operatore As List(Of OPERATORI)

    Public Sub New()
    End Sub


    Public Sub New(ByVal negozio As List(Of NEGOZI), ByVal cassa As List(Of CASSE), ByVal operatore As List(Of OPERATORI))
        Me.negozio = negozio
        Me.cassa = cassa
        Me.operatore = operatore
    End Sub

    Public Class NEGOZI

        Public Property NPV As Integer

        Public Sub New()
        End Sub


        Public Sub New(ByVal NPV As Integer)
            Me.NPV = NPV
        End Sub

    End Class

    Public Class CASSE

        Public Property CS As Integer

        Public Sub New()
        End Sub


        Public Sub New(ByVal CS As Integer)
            Me.CS = CS
        End Sub

    End Class

    Public Class OPERATORI

        Public Property OP As Integer

        Public Sub New()
        End Sub


        Public Sub New(ByVal OP As Integer)
            Me.OP = OP
        End Sub

    End Class

End Class

这是控制器

<HttpPost()>
<Route("post")>
Public Sub PostValue(<FromBody()> ByVal config As Cfg)
    MsgBox(config.negozio(0).NPV)
End Sub

这是我在每个复选框值更改后调用 $.post 的方法

$(".nav").on("change", "input[type='checkbox']", function () {
    var config = {
        negozio: [],
        cassa: [],
        operatore: []
    };


    $(this).siblings('div').children('ul')
        .find("input[type='checkbox']")
        .prop('checked', this.checked);

    $("input[type='checkbox']:checked").each(function () {
        const npv = $(this).attr('data-npv');
        const cs = $(this).attr('data-cs');
        const op = $(this).attr('data-op');

        if (npv != null)
            config.negozio.push(npv);

        if (cs != null)
            config.cassa.push(cs);

        if (op != null)
            config.operatore.push(op);
    });
    console.log(config)
    $.post("api/prodotti/post/", config);
});
4

1 回答 1

0

问题是我在没有键的情况下传递值 npv、cs、op,通过添加它所有的工作原理,

所以javascript代码更改如下:

$(".nav").on("change", "input[type='checkbox']", function () {
    var config = {
        negozio: [],
        cassa: [],
        operatore: []
    };


    $(this).siblings('div').children('ul')
        .find("input[type='checkbox']")
        .prop('checked', this.checked);

    $("input[type='checkbox']:checked").each(function () {
        const npv = $(this).attr('data-npv');
        const cs = $(this).attr('data-cs');
        const op = $(this).attr('data-op');

        if (npv != null)
            config.negozio.push({ NPV: npv });

        if (cs != null)
            config.cassa.push({ CS: cs });

        if (op != null)
            config.operatore.push({ OP: op });
    });
    console.log(config)
    $.post("api/prodotti/post/", config);
});
于 2019-12-16T13:49:54.377 回答