Reputation: 5
I've faced some problem recently and couldn't find the solution. I'm working on SportsStore from Adam Freeman's book Pro MVC 4. Look at this please:
I have a View called 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>
and CartIndexModelView:
public class CartIndexViewModel
{
public Cart Cart { get; set; }
public string ReturnUrl { get; set; }
}
And my problem is actually that my <a href="@Model.ReturnUrl">Kontynuuj zakupy</a>
returns empty <a>KontunuujZakupy</a>
Html, which I guess means that @Model.ReturnUrl
doesn't get any value at all. I couldn't figure out why because i am begginer, would You mind to give me a clue about that? Thanks.
//edit
"Kontynuuj zakupy" means Continue Shopping :)
Upvotes: 0
Views: 188
Reputation: 483
you can simply change the last line of your AddToCart action to:
return RedirectToAction("Index", new { returnUrl = returnUrl });
Upvotes: 0
Reputation: 118947
Your index action looks like this:
public ViewResult Index(string returnUrl) { ... }
It takes the parameter of returnUrl
and insert that into the model which you return. If you browse to your website without specifying a return URL, it will be blank, for example:
http://localhost:1234
http://localhost:1234/Home/Index
Try passing a parameter like this:
http://localhost:1234?returnUrl=xxxx
http://localhost:1234/Home/Index?returnUrl=xxxx
Notice that the parameter name matches the index action. So in your AddToCart
and RemoveFromCart
actions, you need to change the name of the parameter from url
to returnUrl
.
return RedirectToAction("Index", new { returnUrl = returnUrl });
Upvotes: 1