Good day,
I am trying to use ajax.beginform in my asp.Net MVC project, to create an alert on success. The problem is that I could not make it work, I am not getting any error message neither.
[HttpPost]
public ActionResult Create(Details details)
{
//do something
//add details to db
return View("Create", details);
}
public class Details
{
public string Name { get; set; }
public string Email { get; set; }
}
This is my view
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
@using (Ajax.BeginForm("Create", new AjaxOptions()
{
OnSuccess= "ajaxSuccess"
}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Please fix the following errors.")
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
@section scripts
{@Scripts.Render("~/bundles/jqueryval")
<script>
function ajaxSuccess () { alert('this is ajaxSuccess'); }
$(document).ready(function ()
{
function ajaxSuccess () { alert('this is ajaxSuccess'); }
});
</script>
}
As you can see, I am using Jquery.unobstrusive-ajax.min.js
script. My action is completed and returns to the view, but there is no error in my console, neither an alert. Am I missing something?
Update: I added an alert to the document ready, but did not trigger **Update 2: ** changed function to function ajaxSuccess () { alert('this is ajaxSuccess'); }