I want to pass the parameter which is rentalPeriod using Html.BeginForm.
Currently, I put just text in Details.cshtml.
If I want to pass the rentalPeriod such as 3, 7, 14 as a number, what do I have to do?
Do I have to use hiddenfield or a function?
I don’t know how to pass the parameter to my other method which is AddToCart method.
Please let me know how to pass the value of the number.
Thanks.
Details.cshtml.
<td>
Rental Period: <br />
@using (Html.BeginForm("AddToCart", "ShoppingCart", new { id = Model.productId }, FormMethod.Post))
{
<div class="display-label">
@Html.RadioButtonFor(model => model.price, Model.threeDayPrice)
//I put just text in here '3 day' How can I write this number, '3'?
//do I have to use hiddenfield? or other functions?
3 day: £@Model.threeDayPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, Model.aWeekPrice)
1 week: £@Model.aWeekPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, @Model.twoWeekPrice)
2 week: £@Model.twoWeekPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, @Model.aMonthPrice)
1 month: £@Model.aMonthPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, @Model.threeMonthPrice)
3 month: £@Model.threeMonthPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, @Model.sixMonthPrice)
6 month: £@Model.sixMonthPrice
</div>
<br />
<input type="submit" class="button" value="Add to cart" style="margin-left:0px; width:90px;"/>
}
</td>
ShoppingCart controller in AddToCart method.
//How to pass the parameter here.
[HttpPost]
public ActionResult AddToCart(int id, FormCollection col)
{
var addedProduct = db.Product
.Single(product => product.productId == id);
decimal priceValue = Convert.ToDecimal(col["price"]);
// Retrieve the product from the database
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedProduct);
// Go back to the main store page for more shopping
return RedirectToAction("Index", new { @priceValue = priceValue });
}
I strongly suggest that you use View Models rather than relying on the FormsCollection. However, yes.. in your case you can just use a hidden field to store the item, then you pull it out of the FormsCollection.