Red Swan
Red Swan

Reputation: 15545

multiple button click in asp.net MVC 3

I am having multiple dynamic buttons on my asp.net mvc 3 page. what is the best way to handle button click in asp.net mvc 3? there is no event handling in asp.net, so what is the best practice to hadle.?

Upvotes: 1

Views: 6762

Answers (3)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

If the buttons do not require the same form data, then you can create two forms with different action methods. This is the easiest solution.

If you need to use the same form data, then there are a number of methods, inclduing Darin and tvanfosson's approaches. There is also an approach based on attributes that will select the correct action method based on which button is clicked.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=724

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532455

Depends on what the buttons are doing. If they are logically separate actions, then you could have each postback to a separate action on the server side. This often also works they are variants of the same action, Save vs. Cancel, for instance where Save posts back the form and Cancel redirects to you the previous url (say, going back to details from edit). If the buttons represent different data that would get posted back to the same action, you can give them different values. If the buttons are named, the values will get posted back along with the rest of the form, assuming they are included in the form. If posting back from AJAX, you might need to explicitly serialize the button value along with the form.

Example of Save/Cancel

@using (Html.BeginForm())
{
   //...
   <button type="submit" class="submit-button button">Save</button>
   @Html.ActionLink( "Cancel", "details", new { ID = Model.ID }, new { @class = "cancel-button button" } )
}

Then use CSS, perhaps in conjunction with jQuery UI to style the buttons.

<script type="text/javascript">
   $(function() {
       $('.button').button();
       ...
   });
</script>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

You could handle the buttons clicks using javascript by subscribing to their click event. For example with jQuery you could give those buttons a class and then:

$(function() {
    $('.someClass').click(function() {
        // a button was clicked, this will point to the actual button
    });
});

or if those are submit buttons of a form you could give them the same name and different values and then on the server test the value of the name parameter. It's value will equal to the button that was clicked.

Let's suppose for example that you have the following form with multiple submit buttons:

@using (Html.BeginForm())
{
    ... some input fields

    <button type="submit" name="Button" value="delete">Delete data</button>
    <button type="submit" name="Button" value="save">Save data</button>
}

Now inside the controller action you are posting to you could determine which button was clicked:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    var button = Request["button"];
    if (button == "save")
    {
        // the save button was clicked
    }
    else if (button == "delete")
    {
        // the delete button was clicked
    }

    ...
}

Upvotes: 2

Related Questions