2

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);
             }
         });
     } 
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

1 Answers1

3

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);            
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • In another thread (guess which one :P ) you wrote I could overwrite the RemoteAttribute.IsValid is that also valid for my case? – Elisabeth May 30 '12 at 19:34
  • Yes, you can override it and in this case ModelState.IsValid will be set to the correct value. – Darin Dimitrov May 30 '12 at 19:36
  • this has the effect that I do not need the ReleaseExists call in the Create action, BUT could there be a race condition like user leaves the textbox with "test" data clicks on the OK button at that time my Create action could be faster called than the ReleaseExists action can return false. Do you think the same? – Elisabeth May 30 '12 at 19:39
  • I read your comment again Darin. "That's normal, Remote validation rules are applied only when you invoke the controller action through AJAX." But I do an Ajax call. Or do you mean that generally Remote validation is only possible on Client side and NEVER Server side? – Elisabeth May 31 '12 at 17:39