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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • 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)`? –  Jun 21 '16 at 03: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? – Wai Yan Hein Jun 21 '16 at 05:41
  • You have not shown the relevant parts of the view so cannot help :). –  Jun 21 '16 at 05:43
  • Yes. I edited my question now adding new article. Please help me. – Wai Yan Hein Jun 21 '16 at 05: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? –  Jun 21 '16 at 05:54
  • It is already included built in ASP.NET – Wai Yan Hein Jun 21 '16 at 05:59
  • What is _already included built in ASP.NET_? What are you referring to? I asked you to show the view and instead you show an image of something unrelated to your question. –  Jun 21 '16 at 06:00
  • It is already included with jquery 3.0.0. It is validating required field using javascript. Not to remote. When I enter a value for name and click submit. It is not working at all. – Wai Yan Hein Jun 21 '16 at 06:03
  • Still cant see your view. Best of luck getting help. –  Jun 21 '16 at 06:09
  • I am sorry. I was spinning around with that error. I added view. Really sorry. – Wai Yan Hein Jun 21 '16 at 06:13
  • Editing view and running it again. That is why – Wai Yan Hein Jun 21 '16 at 06:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115169/discussion-between-stephen-muecke-and-wai-yan-hein). –  Jun 21 '16 at 06:21
  • Hi @StephenMuecke . I solved the problem. The problem was with Jquery. There is an reference link to Jquery old version before 3.0.0. I run update-ackage command. I think that s why. I removed old one. Now it is working. Thanks so much for ur support. Really appreciate. – Wai Yan Hein Jun 21 '16 at 08:49
  • Another reason to always use bundles to generate your scripts ;) –  Jun 21 '16 at 08:51
  • Thanks so much. That was totally my fault. – Wai Yan Hein Jun 21 '16 at 08:54
  • Can be closed under the official close reason: _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers._ – halfer Jun 16 '19 at 10:36

0 Answers0