簡體   English   中英

ReturnUrl沒有值,ASP.Net MVC

[英]ReturnUrl has no value, ASP.Net MVC

我最近遇到了一些問題,找不到解決方案。 我正在使用亞當·弗里曼(Adam Freeman)的Pro MVC 4書中的SportsStore。

我有一個稱為索引的視圖:

@model WebUI.Models.CartIndexViewModel
.
.
.
<p align="center" class="actionButtons">
    <a href="@Model.ReturnUrl">Kontynuuj zakupy</a>
</p>

CartController:

{
    public class CartController : Controller
    {
        private IProductRepository repository;

        public CartController(IProductRepository repo)
        {
            repository = repo;
        }

        public ViewResult Index(string returnUrl)
        {
            return View(new CartIndexViewModel
            {
                Cart = GetCart(),
                ReturnUrl = returnUrl
            });
        }

        public RedirectToRouteResult AddToCart(int productID, string returnUrl)
        {
            Product product = repository.Products.FirstOrDefault(p => p.ProductID == productID);
            if (product != null)
            {
                GetCart().AddItem(product, 1);
            }
            return RedirectToAction("Index", new { url = returnUrl });
        }

        public RedirectToRouteResult RemoveFromCart(int productId, string returnUrl)
        {
            Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (product != null)
            {
                GetCart().RemoveLine(product);
            }
            return RedirectToAction("Index", new { url = returnUrl });
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

    }
}

產品摘要視圖:

@model Domain.Entities.Product

<div class="item">
    <h3>@Model.Name</h3>
    @Model.Description
     @using (Html.BeginForm("AddToCart", "Cart"))
    {
        @Html.HiddenFor(x => x.ProductID)
        @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
        <input type ="submit" value="+ Dodaj do koszyka"/>
    }
    <h4>@Model.Price.ToString("c")</h4>
</div>

和CartIndexModelView:

public class CartIndexViewModel
{
    public Cart Cart { get; set; }
    public string ReturnUrl { get; set; }
}

我的問題實際上是我的<a href="@Model.ReturnUrl">Kontynuuj zakupy</a>返回空的<a>KontunuujZakupy</a> HTML,我想這意味着@Model.ReturnUrl沒有得到任何結果價值。 我不知道為什么,因為我是乞gg,您介意給我一個線索嗎? 謝謝。 // edit“ Kontynuuj zakupy”表示繼續購物:)

您的索引操作如下所示:

public ViewResult Index(string returnUrl) { ... }

它采用returnUrl參數並將其插入您返回的模型中。 如果在未指定返回URL的情況下瀏覽到您的網站,它將為空白,例如:

http://localhost:1234
http://localhost:1234/Home/Index

嘗試傳遞這樣的參數:

http://localhost:1234?returnUrl=xxxx
http://localhost:1234/Home/Index?returnUrl=xxxx

請注意,參數名稱與索引操作匹配。 因此,在您的AddToCartRemoveFromCart操作中,您需要將參數的名稱從url更改為returnUrl

return RedirectToAction("Index", new { returnUrl = returnUrl });

您只需將AddToCart操作的最后一行更改為:

 return RedirectToAction("Index", new { returnUrl = returnUrl });

暫無
暫無

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

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