I have implemented an ajax form in a MVC3 project and I am looking for a way to incorporate a JavaScript confirm popup upon clicking the submit button. While I have managed to get the popup to display, I am struggling to prevent the form submission if the user selects the cancel option.
Can anyone suggest the most effective approach to achieve this?
I have experimented with the following JavaScript code...
function showWarning(e) {
var answer = confirm('message');
if (answer) {
return true;
}
return false;
}
However, due to the asynchronous nature of ajax, this method is not functioning as desired.
Instead of coding the ajax call myself, I am utilizing the ajax.begin form helper as shown below...
using (Ajax.BeginForm("LinkName", "Account", null, new AjaxOptions { UpdateTargetId = "divLinkName", HttpMethod = "POST" }, new { @id="formLinkName" }))
{
<div class="row">
<div class="large-12 columns">
<div class="editor-label">
@Html.LabelFor(m => m.LinkName)
@Html.TextBoxFor(m => m.LinkName)
@Html.ValidationMessageFor(m => m.LinkName)
<input type="submit" value="Set my Link Name" class="button radius small" />
</div>
</div>
</div>
}
If you have any insights or suggestions on how to address this issue, please share them. Thank you.