Razor中具有多个参数的ajaxpost

Razor中具有多个参数的ajaxpost,ajax,asp.net-core,post,razor,Ajax,Asp.net Core,Post,Razor,在ASP.NETCore+Razor中,我想用多个参数发布一篇ajax文章。参数的数量可能会有所不同。我有带ID的产品,每个产品ID都有一个数量。我想将此数据发送到服务器。将创建ProductAndAmount元组并将其字符串化。ajax文章中的数据是可用的。但是OnPostReduceProducts()方法中的参数1为null。我还用了防伪代币。 为什么参数1仍然为空 BuyProducts.js文件 function buyProducts(nItems) { document.

在ASP.NETCore+Razor中,我想用多个参数发布一篇ajax文章。参数的数量可能会有所不同。我有带ID的产品,每个产品ID都有一个数量。我想将此数据发送到服务器。将创建ProductAndAmount元组并将其字符串化。ajax文章中的数据是可用的。但是OnPostReduceProducts()方法中的参数1为null。我还用了防伪代币。 为什么参数1仍然为空

BuyProducts.js文件

function buyProducts(nItems) {

    document.getElementById("buyButton").innerHTML = "bought products";

    var totalPriceOfAllProductsLabel = document.getElementById('totalPriceLabel');
    totalPriceOfAllProductsLabel.textContent = "0.00";

    // reduce items from stock    
    var objectArray = new Array();
    var tuple = Object.freeze({ Id: "1", Amount: "2" });
    objectArray.push(tuple);
    var dataToPost = JSON.stringify({ parameter1: objectArray });

    $.ajax({
        type: 'POST',
        async: false,
        url: "/StockItems/Edit?handler=ReduceProducts",
        data: dataToPost,
        dataType: "json",
        headers: {
            RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
        }
    });
}
ajaxpost中的数据字段

{"parameter1":[{"Id":"1","Amount":"2"}]}
我的Edit.cshtml的一部分

@section Scripts {
    <script src="~/js/BuyProducts.js"></script>
}
@节脚本{
}
部分my Edit.cshtml.cs

public void OnPostReduceProducts(List<ProductAndAmount> parameter1)
{
    System.Diagnostics.Debug.WriteLine("OnPostReduceProducts() is running");
    System.Diagnostics.Debug.WriteLine(parameter1[0].Id);
    System.Diagnostics.Debug.WriteLine(parameter1[0].Amount);
}

public class ProductAndAmount
{
    public string Id { get; set; }
    public string Amount { get; set; }
}
PostReduceProducts上的公共无效(列表参数1) { System.Diagnostics.Debug.WriteLine(“OnPostReduceProducts()正在运行”); System.Diagnostics.Debug.WriteLine(参数1[0].Id); System.Diagnostics.Debug.WriteLine(参数1[0].Amount); } 公共类产品和数量 { 公共字符串Id{get;set;} 公共字符串金额{get;set;} } 我是否提供了回答此问题所需的所有代码部分?

更改

var dataToPost = JSON.stringify({ parameter1: objectArray });

更改ajax(添加contenttype):

更改操作:

public void OnPostReduceProducts([FromBody]List<ProductAndAmount> parameter1)
{
    System.Diagnostics.Debug.WriteLine("OnPostReduceProducts() is running");
    System.Diagnostics.Debug.WriteLine(parameter1[0].Id);
    System.Diagnostics.Debug.WriteLine(parameter1[0].Amount);
}
PostReduceProducts([FromBody]列表参数1)上的公共无效 { System.Diagnostics.Debug.WriteLine(“OnPostReduceProducts()正在运行”); System.Diagnostics.Debug.WriteLine(参数1[0].Id); System.Diagnostics.Debug.WriteLine(参数1[0].Amount); } 更新:


如果仍然不起作用,您可以删除缓存并重试。

我尝试了此操作,但C#方法中的参数1仍然为空。我已经更新了我的答案,您还需要更改ajax和操作。我不知道为什么。在浏览器中,它告诉我参数1为空。但是我删除了ajax中的参数1。这是浏览器缓存问题吗?{“parameter1”:[{“Id”:“1”,“Amount”:“2”}]}在数据字段中!但是这个参数1是从哪里来的呢?我只在C#方法中使用它。因为模型将模型绑定到操作,所以不需要告诉模型名称,它会自动将传递的数据绑定到OnPostReduceProducts中的参数1。您可以使用答案吗?
$.ajax({
        type: 'POST',
        async: false,
        url: "/StockItems/Edit?handler=ReduceProducts",
        data: dataToPost,
        dataType: "json",
        contentType: 'application/json',
        headers: {
            RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
        }
    });
public void OnPostReduceProducts([FromBody]List<ProductAndAmount> parameter1)
{
    System.Diagnostics.Debug.WriteLine("OnPostReduceProducts() is running");
    System.Diagnostics.Debug.WriteLine(parameter1[0].Id);
    System.Diagnostics.Debug.WriteLine(parameter1[0].Amount);
}