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 t开发者_StackOverflow中文版he 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?
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();
}
精彩评论