views:

53

answers:

1

i have the following form

<form action="ManageLink" method="post">
    <input name="artistName" type="text"/>
    <input name="songName" type="text"/>
    <input name="url" type="text"/>
    <input name="action" id="save" type="submit" value="Save"/>
    <input name="action" id="delete" type="submit" value="Delete"/>
</form>

with this signature for the action method

public JavaScriptResult ManageLink(string artistName, string songName, string url, string action)

I'm naming the submit buttons so that I can know which one was clicked, and act accordingly.

I'm trying to turn this into an ajax form as shown below

<% using (Ajax.BeginForm("ManageLink", new AjaxOptions()))
   { %>
        <input name="artistName" type="text"/>
        <input name="songName" type="text"/>
        <input name="url" type="text"/>

        <input name="action" type="submit" value="Save"/>
        <input name="action" type="submit" value="Delete"/>
<% } %>

however i get an error in the MicrosoftAjax.js code.

If i remove the name="action" property then i dont get the error, but then I'm not able to tell which button was clicked.

Is there something I've got wrong in the code above? Or is there a better approach that i can use to detect which button was clicked?

A: 

Try this:

<input name="save" type="submit" value="Save" />
<input name="delete" type="submit" value="Delete" />

and in your action:

public ActionResult ManageLink(string artistName, string songName, string url)
{
    if (!string.IsNullOrEmpty(Request["save"]))
    {
        // Save was clicked
    }
    else if (!string.IsNullOrEmpty(Request["delete"]))
    {
        // Delete was clicked
    }
    return View();
}

or if you prefer action arguments:

public ActionResult ManageLink(string artistName, string songName, string url, string save, string delete)
{
    if (!string.IsNullOrEmpty(save))
    {
        // Save was clicked
    }
    else if (!string.IsNullOrEmpty(delete))
    {
        // Delete was clicked
    }
    return View();
}
Darin Dimitrov
Thanks, used your solution and added some code from herehttp://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/ to add an action method per submit button
Fikre