I have actionResult method in controller which is been called via ajax javascript function which display form. This partial view appears in web page along with other part in div with class="webPage_content_block". Now I am require to do the server validation for form field, in case any of field incorrect, partial view is pass along with model, but where my issue is in ajax function. what I want if everything is ok in form, controller pass json message return Json(new { Response = "Success" }) and in ajax done method if I get success then redirect to different page i.e. view else alert message
[HttpGet]
public ActionResult CreateNewFunctionNavigation()
{
return PartialView("CreateNewNavigation_Partial");
}
[HttpPost]
public ActionResult CreateNewFunctionNavigation(CreateFunctionNavigation_SP_Map obj )
{
if(ModelState.IsValid)
{
try
{
_FN_Services_a2.CreateFunctionNavigation(obj);
return Json(new { Response = "Success" });
}
catch (DataException ex)
{
ModelState.AddModelError("", "Unable To Create New Function Navigation" + ex);
}
}
return PartialView("CreateNewNavigation_Partial", obj);
} //end
ajax
$('#NewFunctionNavigationForm').submit(function (e) {
e.preventDefault();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
}).done(function (result) {
$('.webPage_content_block').html(result);
});
});
razor form
@using (Html.BeginForm("CreateNewFunctionNavigation", "SystemCore", FormMethod.Post, new { id = "NewFunctionNavigationForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
//form fields here....
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default _formButton" />
<input type="button" value="Cancel" class="btn btn-default _formButton" onclick="CancelPage();" />
</div>
</div>
}