I am trying to throw up a confirmation dialog before a user clicks delete.
@using (Html.BeginForm("Delete", "Controller", new { viewModel.Id }, FormMethod.Post, null, new { onsubmit= "return confirm('Do you really want to submit the form?');", @style = "text-align: center" }))
{
<input type="submit" value="X" class="form-control btn btn-danger" />
}
The form seems to be rendering as you would expect:
<form action="/admin/RiskProfile/Delete/24" method="post" onsubmit="return confirm('Do you really want to submit the form?');" style="text-align: center">
<input type="submit" value="X" class="form-control btn btn-danger">
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8O5vcsGUmyZOqz2RSFC3UgK8ICB1W1Ov79zW2-IkboTHIL_LvzQMkjy9s4JsbrA9fEXtE4YfWy1pULXxUk4VhKJc2V53WUuVYJwTB0gbBlxRmM8flrHnFvmtJ8Dr_6zPXmDkZW31Tga5pEiCPw1ebTQMbCLmbhzLKKf9jLErmbajbpUSpYUtZG5H_bMaXw3ptg">
</form>
When I click the button it simply submits the form without throwing up the confirmation dialog.
My knowledge of .net is limited, but it seems like you just need to prevent the default action from the form submission event. Here's some code to get you rolling, though it may not be perfect:
@using (Html.BeginForm("Delete", "Controller", new { viewModel.Id }, FormMethod.Post, null, new { onsubmit= "onFormSubmit", @style = "text-align: center" }))
{
<input type="submit" value="X" class="form-control btn btn-danger" />
}
function onFormSubmit (e) {
e.preventDefault() // The default event action should be ignored
return confirm('Do you really want to submit the form?');
}