C# ASP.NET Core MVC数据库在重定向后不添加对象

C# ASP.NET Core MVC数据库在重定向后不添加对象,c#,asp.net,asp.net-core,asp.net-mvc-4,asp.net-web-api,C#,Asp.net,Asp.net Core,Asp.net Mvc 4,Asp.net Web Api,我这里有两种情况。第一种情况发生在我路由到的页面存在时,另一种情况发生在页面不存在时。有点奇怪 我试图在asp.net core中将一个对象添加到我的数据库中,但当我单击页面上的“订单”按钮时,它会将我重定向到应用程序的索引页面(没有什么大问题可以解决),但我的问题是,我在POST请求中发送的对象没有添加到数据库中 另一方面,如果页面不存在,则对象将添加到数据库(没有问题),但我收到一个内部服务器错误,该错误表示在搜索页面时使用的文件夹中找不到“索引”。我的问题是,如何仍然将对象添加到数据库中,

我这里有两种情况。第一种情况发生在我路由到的页面存在时,另一种情况发生在页面不存在时。有点奇怪

我试图在asp.net core中将一个对象添加到我的数据库中,但当我单击页面上的“订单”按钮时,它会将我重定向到应用程序的索引页面(没有什么大问题可以解决),但我的问题是,我在POST请求中发送的对象没有添加到数据库中

另一方面,如果页面不存在,则对象将添加到数据库(没有问题),但我收到一个内部服务器错误,该错误表示在搜索页面时使用的文件夹中找不到“索引”。我的问题是,如何仍然将对象添加到数据库中,并仍然路由到我要路由到的页面(例如,感谢您的购买页面)

现在,我将提供我使用的控制器的代码和端点代码:

端点:

app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
控制器:

[路线(“api/购物”)]
[ApiController]
公共类SendItemsController:控制器
{
私有AppDbContext_db;
公共SendItemsController(AppDbContext数据库)
{
_db=db;
}
[HttpPost]
[使用(“应用程序/json”)]
公共异步任务保存([FromBody]ShoppingCart s)
{
wait _db.ShoppingCarts.AddAsync(s);
等待_db.SaveChangesAsync();
返回至Topage(“索引”);
}
公共IActionResult索引()
{
返回视图();
}
}

命令
以下是我在购物车页面上使用的javascript:

  var orderB = document.getElementById("orderB");
        orderB.addEventListener("click", function () {
            var inputName = document.getElementById("inputName").value;
            var inputAddress = document.getElementById("inputAddress").value;
            var inputMail = document.getElementById("inputMail").value;
            var auxArray = [];
            for (var i = 0; i < productsAux.length; i++) {
                if (productsAux[i]!="") {
                auxArray[i-1] = { "productName": productsAux[i].titlu, "productPrice": productsAux[i].pret, "quantity": localStorage.getItem(productsAux[i].titlu) };
                }
            }
            var shoppingCart = {
                productList: auxArray,
                clientName: inputName,
                clientAddress: inputAddress,
                clientMail: inputMail
            };
            
            $.ajax({
                type: "POST",
                data: JSON.stringify(shoppingCart),
                url: "api/Shopping",
                contentType: "application/json;charset=utf-8",
            })
        })
var orderB=document.getElementById(“orderB”);
orderB.addEventListener(“单击”,函数(){
var inputName=document.getElementById(“inputName”).value;
var inputAddress=document.getElementById(“inputAddress”).value;
var inputMail=document.getElementById(“inputMail”).value;
var auxArray=[];
对于(var i=0;i
RedirectToPage
用于Razor页面,虽然这是一个MVC项目,但我认为您需要使用
RedirectToAction

当我点击页面上的订单按钮时,它会将我重定向到应用程序的索引页面

因此,您想通过单击订单按钮触发
SendItemsController
中的
Save
操作吗?请求是什么样子的,url正确吗?也许您可以向我们显示客户端代码

我做了一个简单的演示和测试与邮递员,你可以有一个参考

[HttpPost]
[Consumes("application/json")]
public async Task<IActionResult> Save([FromBody] ShoppingCart s)
{
    await _db.ShoppingCarts.AddAsync(s);
    await _db.SaveChangesAsync();
    return RedirectToAction("Thanks");
}

[HttpGet("Thanks")]
public IActionResult Thanks()
{
    return View();
}
[HttpPost]
[使用(“应用程序/json”)]
公共异步任务保存([FromBody]ShoppingCart s)
{
wait _db.ShoppingCarts.AddAsync(s);
等待_db.SaveChangesAsync();
返回操作(“谢谢”);
}
[HttpGet(“谢谢”)]
公众谘询结果谢谢()
{
返回视图();
}

更新:

@section scripts{
    <script>
        var orderB = document.getElementById("orderB");
        orderB.addEventListener("click", function (e) {
            e.preventDefault();
            var inputName = document.getElementById("inputName").value;
            var inputAddress = document.getElementById("inputAddress").value;
            var inputMail = document.getElementById("inputMail").value;
            var auxArray = [];
            for (var i = 0; i < productsAux.length; i++) {
                if (productsAux[i] != "") {
                    auxArray[i - 1] = { "productName": productsAux[i].titlu, "productPrice": productsAux[i].pret, "quantity": localStorage.getItem(productsAux[i].titlu) };
                }
            }
            var shoppingCart = {
                productList: auxArray,
                clientName: inputName,
                clientAddress: inputAddress,
                clientMail: inputMail
            };

            $.ajax({
                type: "POST",
                url: "/api/Shopping",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(shoppingCart),

            })
        })
    </script>
}
@节脚本{
var orderB=document.getElementById(“orderB”);
orderB.addEventListener(“单击”,函数(e){
e、 预防默认值();
var inputName=document.getElementById(“inputName”).value;
var inputAddress=document.getElementById(“inputAddress”).value;
var inputMail=document.getElementById(“inputMail”).value;
var auxArray=[];
对于(var i=0;i
如果它在该代码中指向重定向,则该对象将被保存。听起来您需要附加一个调试器,并验证此方法是否被命中。@mason它不是第一次被命中,但如果我转到我的购物车页面并再次点击按钮,它将被命中。我试图按照您所说的做,但现在我得到了415“此页面不工作”我为我使用的表单添加了html。另一方面,如果我有方法
public IActionResult Index(){return View();}@section scripts{
    <script>
        var orderB = document.getElementById("orderB");
        orderB.addEventListener("click", function (e) {
            e.preventDefault();
            var inputName = document.getElementById("inputName").value;
            var inputAddress = document.getElementById("inputAddress").value;
            var inputMail = document.getElementById("inputMail").value;
            var auxArray = [];
            for (var i = 0; i < productsAux.length; i++) {
                if (productsAux[i] != "") {
                    auxArray[i - 1] = { "productName": productsAux[i].titlu, "productPrice": productsAux[i].pret, "quantity": localStorage.getItem(productsAux[i].titlu) };
                }
            }
            var shoppingCart = {
                productList: auxArray,
                clientName: inputName,
                clientAddress: inputAddress,
                clientMail: inputMail
            };

            $.ajax({
                type: "POST",
                url: "/api/Shopping",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(shoppingCart),

            })
        })
    </script>
}