WebApi 2 [POST]方法接收到错误的值

时间:2019-12-16 10:29:11

标签: jquery vb.net post webapi

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

enter image description here

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

enter image description here

问题可能与模型有关吗?如果是这样,我如何解决它以获取正确的值? 这是我在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);
});

1 个答案:

答案 0 :(得分: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);
});