Remote Validation works on textbox lost focus, but the ModelState.IsValid is always true

1.5k Views Asked by At

I have a model with RemoteValidation Attribute.

When I enter "test" which already exists in the database and I click on an area except the OK button then I get in red colors: "test already exists." So far so good. When I click then the OK buttona post is done to the Create action where I ask for

ModelState.IsValid which is always true ???

Therefore the data is entered in the database and I get a Duplicate Exception...

I know this worked before on my site, I just have changed some stuff and subversion is not

activated arghhh...

What do I wrong?

[HttpPost]
        public ActionResult Create(Release release)
        {
            if (ModelState.IsValid)
            {
                _releaseDataProvider.AddRelease(release);
                return Json(new { success = true });
            }
            return PartialView(release);            
        }

public JsonResult ReleaseExists(string Name)
        {
            bool releaseExists = _releaseDataProvider.ReleaseExists(Name);
            if (!releaseExists)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }


$.ajaxSetup({ cache: false });

     $(document).ready(function () {
         $('#CreateRelease').click(function (event) { loadDialog(this, event, createRelease); });        
     });

     function loadDialog(link, e, ajaxRequest) {

         e.preventDefault();        
         var $title = link.innerHTML;
         var $contenturl = $(link).attr('href');
         var $dialog = $('<div></div>');
         var $height = $(link).attr('data-dialog-height');
         var $width = $(link).attr('data-dialog-width');

         $dialog.load($contenturl).dialog({
             title: $title,
             autoOpen: true,
             modal: true,
             show: 'fade',
             hide: 'fade',
             width: $width,
             height: $height,
             buttons: {
                 "OK": function () {
                     ajaxRequest($(this), $('form', this));
                 },
                 "Cancel": function () {
                     $dialog.dialog("close");
                 }
             }
         });        
     } 

     function createRelease(dlg, form) { 
         $.ajax({
             url: $(form).attr('action'),
             type: 'POST',
             data: form.serialize(),
             success: function (response) {
                 if (response.success) {
                     dlg.dialog("close"); 
                     // Update UI                    
                 }
                 else {
                     // Reload the dialog with the form to show model/validation errors 
                     dlg.html(response);
                 }
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {              
                 alert(textStatus + '-' + XMLHttpRequest.responseText);
             }
         });
     } 
1

There are 1 best solutions below

4
On BEST ANSWER

ModelState.IsValid which is always true ???

That's normal, Remote validation rules are applied only when you invoke the controller action through AJAX. The same action is not invoked when the form is submitted normally. So it is up to you to call the corresponding validation method in your POST action:

[HttpPost]
public ActionResult Create(Release release)
{
    if (ModelState.IsValid && !_releaseDataProvider.ReleaseExists(release.Name))
    {
        _releaseDataProvider.AddRelease(release);
        return Json(new { success = true });
    }
    return PartialView(release);            
}