I have a form and in this form I have fields that are from a Model. The form is a modal-dialog from bootstrap. When i click on the submit button all fields must be valid. This works! Now i want to close the modal and refresh the page if the Model Validation is valid. Only the code does first the Ajax call and then the ModelState.IsValid
in the Controller.
Ajax:
$('#addAlertModal form').submit(function (e) {
//TODO: if ModelState.IsValid in Controller
refreshAlerts();
closeAlertModal();
});
Does somebody know what i can do about this? Or is this not possible?
EDIT:
Or is it possible to call Ajax functions inside the if
:
@if (ViewData.ModelState.IsValid) {
//Call here the functions
refreshAlerts(); //Error: The name 'refreshAlerts' does not exist in the current context
}
Try using ViewData.ModelState.IsValid
in a view
$('#addAlertModal form').submit(function (e) {
@if (ViewData.ModelState.IsValid)
{
refreshAlerts();
closeAlertModal();
}
});
Try like this,
$("#YourbuttonId").click(function () {
var validation = $("#FormId");
if (!validation.valid()) {
return false;
}
else {
refreshAlerts();
closeAlertModal();
}
});
Why don't you use an event so that when your validation is ok then you close the page and refresh. you'll need to use a trigger that's all.
http://api.jquery.com/category/events/ http://api.jquery.com/trigger/
I have it:
@using (Ajax.BeginForm("Index", "ManageAlert", FormMethod.Post, new AjaxOptions { UpdateTargetId = "addAlertModal", InsertionMode = InsertionMode.Replace, OnSuccess = "afterSubmit(data)"}))
{ ... }
function afterSubmit(data) {
if (data.success === true) {
refreshAlerts();
closeAlertModal();
}
}
And in the Model i return:
return Json(new { success = true });