我在 ASP.NET MVC 中創建一個購物網站。
我想要做的是當用戶單擊“添加到購物車”按鈕時顯示一條訊息“您的產品已添加到購物車”。我不知道如何在任何頁面上重定向我只是顯示一條訊息。
這是我的控制器
public ActionResult AddtoCart(int id)
{
List<Product> list;
if (Session["MyCart"] == null)
{
list = new List<Product>();
}
else
{
list = (List<Product>)Session["MyCart"];
}
list.Add(db.Products.Where(p => p.Product_ID == id).FirstOrDefault());
Session["MyCart"] = list;
list[list.Count - 1].Pro_Quantity = 1;
ViewBag.cart = list.Count();
Session["count"] = Convert.ToInt32(Session["count"]) 1;
return RedirectToAction("Shop", "Home");
}
這是我的“加入購物車”按鈕:
<a href="@Url.Action("AddtoCart","Home", new {id=p.Product_ID })" class="filled-button">Add to Cart</a>
uj5u.com熱心網友回復:
實作此目的的一種常見方法是ViewBag在控制器操作中添加一個值,然后在該操作回傳的視圖中,檢查該ViewBag值是否已設定,如果已設定,則顯示您的訊息。
但是,ViewBag僅持續單個請求的持續時間。A RedirectorRedirectToAction是一個新的請求。要在 MVC 中的一個請求和另一個請求之間發送資料,您必須使用TempData。
(如果您想了解更多關于何時使用ViewBag和TempData使用以及它們的生命周期,請參閱ViewBag/ViewData 生命周期)。
因此,由于您的控制器操作回傳RedirectToAction,您必須TempData在第一個操作中使用。所以在你的AddToCart行動中,你會添加:
TempData["ItemAdded"] = true;
然后,在Shop操作中,您可以獲取該TempData值并使用它在 中設定一個值,ViewBag以便它可以顯示在視圖中。
因此,在您的Shop操作中,您可以添加如下內容:
if (TempData.ContainsKey("ItemAdded") && (bool)TempData["ItemAdded"])
{
ViewBag.Message = "Your products are added to cart";
}
最后,在你Shop看來,你會做這樣的事情:
@if (!string.IsNullOrWhiteSpace(ViewBag.Message))
{
<div class="message">
@ViewBag.Message
</div>
}
編輯 - 解決關于想要彈出訊息的評論
如果您想在不離開的情況下更新當前頁面,您將不得不使用 AJAX。
這是實作這一目標的兩種簡單方法。你選擇哪一個取決于你是否已經在頁面上獲得了一個表單,你在這個專案的其他地方是如何完成的,你的公司更喜歡如何完成等等。
1) 使用 jQuery$.ajax()
在您的視圖中,您需要將按鈕更改為以下內容:
<a class="filled-button" onclick="addToCart(event, '@(p.Product_ID)')">
Add to Cart
</a>
<!-- or -->
<button type="button" class="filled-button" onclick="addToCart('@(p.Product_ID)')">
Add to Cart
</button>
(當然,您可以在$(document).ready函式內分配點擊事件,而不是使用onclick屬性。)
同樣在您的視圖中,您將添加一些 JavaScript,如下所示:
var addToCartUrl = "@Url.Action("AddtoCart", "Home")";
// you only need the productId parameter if you're not passing in the event
function addToCart(e, productId) {
// prevent navigating away from the page on <a> tag click
e.preventDefault();
// make ajax request
$.ajax({
url: addToCartUrl,
data: { id: productId },
success: function (data) {
// display your message however you want
}
});
}
在您的控制器操作中:
// change the last line from:
// return RedirectToAction("Shop", "Home");
// to:
return Json("Your products are added to cart", JsonRequestBehavior.AllowGet);
2)使用MVC內置的ajax形式
This method isn't that drastically different from the previous method.
First, you'll surround the product section of your View with a form, like so:
@using (Ajax.BeginForm("AddtoCart", "Home", new AjaxOptions { OnSuccess = "productAddedToCart" }))
{
...
}
And you'll change your "Add to Cart" buttons to submit buttons:
<button type="submit" name="id" value="@(p.Product_ID)" class="filled-button">
Add to Cart
</button>
Just like method #1, you'll need to return JSON from your Controller Action:
// from: return RedirectToAction("Shop", "Home");
return Json("Your products are added to cart", JsonRequestBehavior.AllowGet);
Finally, the OnSuccess JavaScript function will look just like it did in method 1:
function productAddedToCart (data) {
// display your message however you want
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/432105.html
標籤:C# 网 asp.net-mvc asp.net 核心
