【问题标题】:WebApi 2 [POST] method receives wrong valuesWebApi 2 [POST] 方法接收到错误的值
【发布时间】:2019-12-16 10:29:11
【问题描述】:

我正在尝试将一个 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);
});

【问题讨论】:

    标签: jquery vb.net post webapi


    【解决方案1】:

    问题是我传递的值 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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2019-01-16
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      相关资源
      最近更新 更多