I am developing an ASP.NET MVC web application. I am doing remote validation to one of my view model in controller. But it is not working. Remote validation method in controller is not even executed. I searched solutions online and that could not solve my problem. I found these (Remote Attribue Validation not firing in Asp.Net MVC and ASP.NET MVC RemoteAttribute validation not working - Action not being executed) and I tried on it.
This is my model class remote validation applied on Name property
public class CreateCategoryVM
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
[Remote("IsNameUnique","Category",ErrorMessage= "Name must be unique")]
public string Name { get; set; }
[MaxLength(55)]
public string MmName { get; set; }
public IEnumerable<SelectListItem> CategorySelectItems { get; set; }
public int SelectedCategoryId { get; set; }
}
My remote validation method and action method are in the same controller.
This is my create action method and validation method in my controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateCategoryVM model)
{
if(ModelState.IsValid)
{
try
{
Category category = new Category
{
Name = model.Name,
MmName = model.MmName
};
categoryRepo.Create(category);
return RedirectToAction("Create");
}
catch
{
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
}
model.CategorySelectItems = categoryRepo.CategorySelectItems(model.SelectedCategoryId, "Select category", Thread.CurrentThread.CurrentCulture.Name);
return View(model);
}
public JsonResult IsNameUnique(string Name,int Id)
{
if(string.IsNullOrEmpty(Name))
{
return Json(true,JsonRequestBehavior.AllowGet);
}
else
{
var category = categoryRepo.GetCategoryByName(Name);
if(category)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
Required JavaScript settings are enabled in web.config as well
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
But when I submit the form, IsNameUnique action is not executed for remote validation. Everything is working well except from it. New category is also created if validation is passed. Required validation is working also. Only remote validation method is not executed. I tested using break point. How can I fix this?
Update
I added jquery.validate.js and jquery.validate.unobtrusive.js. Then I run and submit the form. Now it is not even submitted. I check JavaScript code and I have no idea how to fix it. I set break point. Not get run when I click submit button.
This is the JavaScript console of browser
This is debugger of browser
This is my view
@using(Html.BeginForm())
{
@Html.AntiForgeryToken();
@Html.ValidationSummary()
@Html.HiddenFor(m=>m.Id)
<div class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-md-3">Name</label>
<div class="col-md-9">
@Html.TextBoxFor(m => m.Name, new { @class="form-control" })
@Html.ValidationMessageFor(m=>m.Name)
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">MM Name</label>
<div class="col-md-9">
@Html.TextBoxFor(m => m.MmName, new { @class="form-control" })
@Html.ValidationMessageFor(m=>m.MmName)
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Parent</label>
<div class="col-md-9">
@Html.DropDownListFor(m => m.SelectedCategoryId, Model.CategorySelectItems, new { @class = "form-control" })
@Html.ValidationMessageFor(m=>m.SelectedCategoryId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
}
How can I get it work?