I have a controller with 2 actions:
public class OrganizationUnitsController : Controller
{
public ActionResult Create()
{
// return empty form
return View();
}
[HttpPost]
public ActionResult Save(OrganizationUnitViewModel unitViewModel)
{
// Save the new unit to the DB
return View();
}
}
The Create.cshtml is:
@using (Html.BeginForm("Save", "OrganizationUnits", FormMethod.Post, new { @id = "form", @class = "form-horizontal" }))
{
<!-- Some inputs -->
<a href="" class="btn btn-primary">Save</a>
}
$('#form')
.on('submit',function (e) {
e.stopPropagation();
$.ajax({
url: '@Url.Action("Save", "OrganizationUnits")',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
Name: $('#Name').val(),
Admin: JSON.stringify({ 'FullDescription': $('#Admin').val() }),
Members: JSON.stringify( $('#users_list_box').val() )
});
});
The problem is when i click the save button, the javascript code which makes the unitViewModel and pass the form to Save action not invoked and the Create action is invoked.
Changing the controller action in the @Html.BeginForm() to null doesn't solved the problem.
How can i fix it?