1

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>&pound;@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?

2
  • Could you give more details about use case? Is it submit form on button click or what? Commented Nov 23, 2012 at 19:57
  • Can you please put ProductsWithDetailModel code also Commented Nov 23, 2012 at 20:04

3 Answers 3

2

ASP.NET MVC takes all inputs and send it by a verb to an action. The bind of these controls is made by the name attribute 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 your ProductsWithDetailModel called compare and it would recevie the value of the button you've clicked.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked, I set the name of the button and, hey presto, it all worked! Thank you!
2

How about having 1 form per item?

E.g.

@foreach (var product in Model.Products)
{
    <tr>
         <td>@product.Provider.Name</td>
         <td>&pound;@Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
         <td>@Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
         <td>
             @using (Html.BeginForm("Compare", "Carousel", FormMethod.Post)) 
             {
                 <input type="hidden" name="productId" value="@product.ProductId"  />
                 <button name="compare">Compare</button>
             }
         </td>
    </tr>
}

1 Comment

I used the below answer and it all worked fine, the ProductsWithDetail model got updated with the "name" property of the button, which is great. But thank you for your answer :)
0

use onclick. set js hidden value in loop. in controller check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.