Shivali Shivali - 22 days ago 9
jQuery Question

Error messages are not getting displayed when trying to post data via JQuery in MVC 5

Error messages are not getting displayed when trying to post data via

JQuery
but the same is happening when I am using
@Html.BeginForm()
.
I am using
jqgrid
here where on selecting user from the grid and clicking on delete all button, the users get deleted and if there is some error then error should be displayed on the screen.

View:

<div >
@Html.ValidationBootstrap()
</div>
<button type='button' `onclick="DeleteAllUsers()"`><span>Delete All</span></button>

function DeleteAllUsers() {
var checkedValues = $('input:checkbox:checked')
.map(function() {
return $(this).val();
})
.toArray();
var act = "@Url.Action("DeleteAllUsers", "Admin")";
$.post(act, { permId: checkedValues }, function (data) {
alert(data.Message);
});
}


Controller:

public ActionResult DeleteAllUsers(List<int> permId)
{
if (permId != null)
{
try
{
foreach (var id in permId)
{
AuthManagerServiceObj.DeleteApplicationPermissionByPermId(id);
}
}
catch (Exception ex)
{
ModelState.AddModelError("", "Error::" + ex.Message);
}
}
else
{
ModelState.AddModelError("", "Error while deleting user :: Please select a user to delete.");
}
return Json(new { Message = "Error" });
}

Answer

It got resolved..instead of calling controller by jquery I called from Html.Begin form and it worked and now I can the errors.

@using (Html.BeginForm("DeleteAllUsers", "Admin", FormMethod.Post, new { area = "", name = "DeleteSelectedUsersForm", id = "DeleteSelectedUsersForm" })) { }