WebApi 2 [POST] 方法收到错误的值
WebApi 2 [POST] method receives wrong values
我正在尝试将 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);
});
问题是我在没有密钥的情况下传递值 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);
});
我正在尝试将 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);
});
问题是我在没有密钥的情况下传递值 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);
});