我有一些我不理解的东西。 我有一个全局函数AjaxPost() 接收url和数据而不是发送到后端。 它调用函数,但参数始终为null。
因此,我采用了该函数的相同内容,并直接在请求中使用了它,它工作得很好。 这个不起作用//示例:AjaxPost(“ / Road / DeleteRoad”,road);
function AjaxPost(url, data) {
return $.ajax({
type: "post",
url: url,
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(data)
});
}
这很完美
$.ajax({
type: "post",
url: "/Road/DeleteRoad",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(road)
});
此处是操作方法
[HttpPost]
public async Task<IActionResult> DeleteRoad([FromBody]r road)
{
int.TryParse(road.RoadID, out int RoadID);
if (RoadID > 0)
{
await _road.DeleteRoad(RoadID);
}
return RedirectToAction("Index");
}
这是r类
public class r
{
public string RoadID { get; set; }
}
这是道路对象 //例如:
var road ={
RoadID :4,
}
答案 0 :(得分:0)
也许您没有正确传递road参数?
以下代码有效,将两个调用发送到服务器。服务器正确检索 RoadID 值。这是javascript:
inventory reach
C#代码为:
Select
[Material]
,[Plnt]
,case
when [calculate 5-year demand] = 0
then 9.01
when [BAAS SO GI (601) 36] = 0
then 9.01
when [MS] <> 'BI' or [MS] <> 'BO'
then ([Stock all SP WH]/([calculate 5-year demand]/5))
when [MS] = 'BO'
then ([Stock all SP WH]/[BAAS SO GI (601) 36])
when [MS] ='BI'
then 0
else 9.01
end as [Inventory Reach]
,case
when [Inventory Reach] > 9
then 1
else 0.9
end as [Devaluation Class]
from [BAAS_PowerBI].[dbo].[Obs]
服务器的输出为:
"use strict";
$(document).ready(() => {
var road = { RoadID: 4 };
// First call
$.ajax({
type: "post",
url: "/home/DeleteRoad",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(road)
});
// Second call
ajaxPost("/home/DeleteRoad", road);
}
function ajaxPost(url, data) {
return $.ajax({
type: "post",
url: url,
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(data)
});
}
希望这会有所帮助:)