Reputation: 311
Wrapped around the axle on this one. So I have a few buttons within a form
@using (Html.BeginForm("Edit", "RunLogEntry", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="create" class="art-button" type="submit" value="Save" />
<input id="create" class="art-button" type="submit" value="Cancel" />
}
What I want to do is when the users click the cancel button, it should hit a different action method which is not the same as in the begin form. I could have the cancel button outside the form but thats not an option since its physical placement needs to be close to rest of the controls of the form.. Or should I use a link button instead of a submit type? Would appreciate any thoughts on this.
Upvotes: 0
Views: 1732
Reputation: 48435
If you don't need to POST any data on 'cancel' then yeah just use a link button with Html.ActionLink method, for example:
@Html.ActionLink("Link Text", "MyAction", "MyController")
You can also use javascript to change the action of a form:
document.myform.action ="/MyController/MyAction";
...this would of course require you to handle the click event of the cancel button with javascript
Upvotes: 4
Reputation: 26
As musefan said, you could just make it a link
If you wanted to keep your button you're going to have to get dirty with some jQuery on this one.
The first fix would be to change the id of the "Cancel" button to something like "cancel" instead of "create" (every single element should have a unique ID).
<input id="cancel" class="art-button" type="submit" value="Cancel" />
Then you need to bind a separate click event to the "Cancel" button to call the different action.
$("#cancel").click(function(e) {
e.preventDefault(); //Keep form from posting
window.location.href = "REDIRECT URL";
//If I want to post
$.post("URL", data { ... });
});
Upvotes: 1