簡體   English   中英

如何用ADO.NET重定向ASP.NET核心中的JSON object

[英]How to redirect the JSON object in ASP.NET core with ADO.NET

在我的項目中,我在創建、編輯和刪除中顯示了彈出窗口。 單擊創建按鈕后輸入詳細信息然后提交,該頁面不會重定向到索引頁面。 但是數據更新了。 為什么這個錯誤。 我用的是 jquery AJAX 投稿。 當我單擊事件時,它不會重定向到索引視圖頁面。

在下面我的 BookController Http Post 操作方法這段代碼有什么錯誤。

圖書控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddOrEdit(int id, [Bind("BookID,Title,Author,Price")] BookViewModel bookViewModel)
{
    if (ModelState.IsValid)
    {
        //Sql Connection
        using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("BookConnection")))
        {
            sqlConnection.Open();
            SqlCommand sqlCmd = new SqlCommand("BookAddOrEdit", sqlConnection);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("BookID", bookViewModel.BookID);
            sqlCmd.Parameters.AddWithValue("Title", bookViewModel.Title);
            sqlCmd.Parameters.AddWithValue("Author", bookViewModel.Author);
            sqlCmd.Parameters.AddWithValue("Price", bookViewModel.Price);
            sqlCmd.ExecuteNonQuery();
        }
        **return RedirectToAction(nameof(Index));
        //return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this,         "Index") });**
    }
    return Json(new {isValid= false,html = Helper.RenderRazorViewToString(this,"AddOrEdit",   bookViewModel) });
}

當你使用 ajax 時,你不能使用 RedirectToAction 重定向到索引頁面。

您可以像下面這樣更改您的退貨方式:

return Json(new { redirectToUrl = Url.Action("Index", "Book") });

並在 Ajax 中添加成功,例如:

success: function (response) {
                window.location.href = response.redirectToUrl;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM