1

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

enter image description here

This is debugger of browser

enter image description here

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?

16
  • 1
    A remote is fired when you change the value in the control. Have you included the relevant scripts (jquery.validate.js and jquery.validate.unobtrusive.js)? And does the view include @Html.ValidationMessageFor(m => m.Name)? Commented Jun 21, 2016 at 3:25
  • Yes, my code missing it. I added jquery.validate and jquery.validate.unobtrusive . But when I click submit button. It is doing nothing at all. Not even submitting the form. I check javascript error from console. There is only two yellow warning lines. So please what is the possible error? Commented Jun 21, 2016 at 5:41
  • You have not shown the relevant parts of the view so cannot help :). Commented Jun 21, 2016 at 5:43
  • Yes. I edited my question now adding new article. Please help me. Commented Jun 21, 2016 at 5:51
  • What does that script have to do with your question (and what is Sizzle)? You need to show you view. And have you included @Html.ValidationMessageFor() for each property? Commented Jun 21, 2016 at 5:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.