I am in MVC environment and I am calling one of my Controller methods from my View. Controller method subsequently does some validation check and returns to the same view with faulty modelstate
At this point, I am expecting all of my validation fields which look like following:
<td>@Html.ValidationMessageFor(m => m.Comments)</td>
to light up with error messages. That is not happening. I think it is because I need to reload the view with new faulty Model. How can I do that?
Following is snipped from my Ajax code:
$("#Save").click(function () {
var model = {
ApplicationNumber: '@Model.ApplicationNumber',
ApplicationId :'@Model.ApplicationId' ,
Name: $('#Name').val(),
CreateDate: $('#CreateDate').val(),
OverrideHireDate: $('#OverrideHireDate').val(),
Amount: $('#Amount').val(),
VendorId: $('#Vendor').val(),
Comments: $('#Comments').val(),
CurrentState: '@Model.CurrentState',
CurrentStatusDate: '@Model.CurrentStatusDate'
};
$.ajax({
data: model,
url: '@Url.Action("SaveApplication", "Applications")',
type: "POST",
success: function (result) {
$(function () {
// some code to activate validation controls?
});
}
});
});
Following is my controller:
public ActionResult SaveApplication(ApplicationModel application)
{
if (!ModelState.IsValid)
{
// ModelState.AddModelError("Name", "This is a Test");
return View("New",application);
}
ApplicationBLL.SaveApplication(application);
return Content(string.Empty);
}
Ok May be this was very trivial but this did the trick.
In the OnSuccess of the AJAX Post I did put following and it worked:
$("body").html(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With