Reputation: 271
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)
Upvotes: 2
Views: 1414
Reputation: 2290
Let me answer Your second question first. @Html has a set of helpers generating html.
@Html.HiddenFor(x => x.ProductID)
will output something like: <input type="hidden" name="ProductID" value="1" />
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
will output something like: <input type="hidden" name="returnUrl" value"/" />
Now look at AddToCart method signature:
AddToCart(Cart cart, int productId, string returnUrl)
Values of these hidden inputs but will be included in the form while posting to AddToCart action and model binding will assign them to productId
and returnUrl
arguments accordingly.
Next to answer first question:
Request.Url.PathAndQuery property returns absolute path of request with query parameters. So if you came to AddToCart view from Index view then returnUrl
will be "/",
because Your Index view is a root of Your webapp, so to speak. It all comes down how routing is configured in Your application.
For more information go watch these tutorials: Controllers and Routing
Upvotes: 2
Reputation: 10211
There is the parameter used by your method controller, the hidden fields is the input fied used to send the paramenters to your server side script
"returnUrl", Request.Url.PathAndQuery
Request.Url.PathAndQuery
will be used by RedirectToAction to establish where to send you
return RedirectToAction("Index", new { returnUrl });
p.ProductID
to manage what you want add to chart
Upvotes: 0
Reputation: 6423
Index is a name that MVC "assumes" so if you go to the index view you won't see
/index
in the url. just /
the 2 hidden fields that you showed are an MVC was of creating
<input type="hidden" ...
if is a way to store information that you need on your view for passing back to the controller through a post back or ajax call
Upvotes: 0