ReturnUrl没有价值,ASP.Net MVC

时间:2014-09-12 09:31:13

标签: asp.net-mvc

我最近遇到了一些问题,无法找到解决方案。我正在从Adam Freeman的书Pro MVC 4开始研究SportsStore。请看这个:

我有一个名为Index的视图:

@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;
        }

    }
}

ProductSummary View:

@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根本没有得到任何值。我无法弄清楚为什么因为我是乞丐,你会介意给我一个线索吗?谢谢。 //编辑 “Kontynuuj zakupy”表示继续购物:)

2 个答案:

答案 0 :(得分:1)

您的索引操作如下所示:

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 });

答案 1 :(得分:0)

您只需将 AddToCart 操作的最后一行更改为:

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