I am trying to submit a form using ajax when in a modal. So far the closest I have come to making that work is this:
@model AppStudio.Data.ViewModels.RolesDetailsViewModel
@section scripts{
@Scripts.Render("~/bundles/jquery.unobtrusive-ajax")
}
<div id="roleDetails" class="modal">
<div class="col-sm-12">
@if (Model.Id == null)
{
<h2>Add Role</h2>
}
else
{
<h2>Edit Role</h2>
}
<hr />
</div>
@using (Ajax.BeginForm("SaveRole", new AjaxOptions() {
InsertionMode = InsertionMode.Replace
}))
{
@Html.AntiForgeryToken()
<div class="col-sm-12">
<div class="form-group">
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.ApplicationId)
</div>
<div class="form-group">
@Html.LabelFor(m => m.RoleName)
@Html.TextBoxFor(m => m.RoleName, new { @class = "form-control col-sm-12" })
@Html.ValidationMessageFor(m => m.RoleName)
</div>
<div class="form-group">
<div class="clearfix">
<button type="submit" class="btn btn-primary pull-right">Save</button>
<a href="#" class="btn btn-default pull-right" rel="modal:close">Cancel</a>
</div>
</div>
</div>
}
</div>
However, when I submit this, it redirects me to a new page with my inputs
I then tried removing the ajax.beginform and replacing it with beginform and changing my submit button to a href link like this:
<a href="#" id="save" class="btn btn-primary pull-right">Save</a>
I also added this script on top:
<script>
$(document).ready(function (e) {
$("#save").click(function () {
alert("test");
});
});
</script>
However, what this results in is the url having a # added to it only. Is there a way to do do ajax submits in a modal? I tried searching around but haven't found much so far.