Search code examples
c#.netajaxasp.net-mvcfluentvalidation

why my Model State is'nt updated when using ajax Post


why my ModelState in controller aren't updated or show true even though there is an error. here is my code

$('#extracontent').on('click', '#Save', function () {
    $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: $('#form').serializeArray(),
            success: ...,
            error: ...
            }
        });
    //}
});

[Authorize]
[HttpPost]
public ActionResult Edit(EditUserModel savedUserModel)
{
    if (!ModelState.IsValid) // model.isValid is true even when there is an error in model
        return PartialView....

I have already try to use TryUpdateModel(savedUserModel) UpdateModel(savedUserModel)

but model state is still valid... the validation do work I already tested using simple POST back


Solution

  • Now I'm embarrassed, after question from markpsmith and begin to paste my model validation I realize that object class that used to render view and and object class that used in parameter is different object, but have very similar properties, that's why binding properties are all filled correctly but validation never executed because object in parameter do not have validation to run....

    anyway changing save controller to correct model solve this problem.