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);
}
});
}