I currently read Freeman's book ASP .NET MVC 4 and do a web application. So, there is a controller named "Cart" with method "AddToCart" and a View. In the view we have such code:
@model SportsStore.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="+ Add to cart" />
}
<h4>@Model.Price.ToString("c")</h4>
</div>
and the method's of the controller code:
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
if (product != null)
{
cart.AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
The code compiles very good, but I have here some questions.
1) If you put a breakpoint on the line:
return RedirectToAction("Index", new { returnUrl });
you'll see that returnUrl has value "/". How? Where did he get it?
2) What are these lines of code doing, because there is no information in the book about it.
@Html.HiddenFor(x => x.ProductID)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)