I’ve currently got a View like this:
@using (Html.BeginForm("Compare", "Carousel", FormMethod.Post))
{
@foreach (var product in Model.Products)
{
<tr>
<td>@product.Provider.Name</td>
<td>£@Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
<td>@Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
<td><button name="compare" value="@product.ProductId">Compare</button</td>
</tr>
}
}
Controller:
[HttpPost]
public ActionResult Compare(ProductsWithDetailModel model) // This is a guess, Model may not persist?
{
// Code here
return View("Index", model);
}
What I actually want is, when a button is clicked, the Controller is going to be called and the product that has been selected (the button clicked) will be added to a list of items.
How do I actually find out which button has been clicked in the Controller? How come I cant find any tutorials out there on the web that have done this before, surely its basic functionality? Or am I approaching it wrong?
ASP.NET MVC takes all inputs and send it by a verb to an action. The bind of these controls is made by the
nameattribute on your View and the value is made by depending of what control we are talking about (text, checkbox, radio, select, button etc…). You could have a a property on yourProductsWithDetailModelcalled compare and it would recevie the value of the button you’ve clicked.