Search code examples
jqueryasp.net-core.net-6.0unobtrusive-validationunobtrusive-ajax

my jQuery-validation not validating the form?


I've made the partial view, and, in my form, I add the jQuery validation and unobtrusive jQuery, but my form returns true for ('form').valid().

There is no error on console side. please can anyone help.

            <form id="addform"
                  @*@Url.Action(@name.ToString())*@
                  data-ajax="true"
                  data-ajax-begin="onSubmit"
                  data-ajax-url="/@name"
                  data-ajax-success="onSuccess"
                  data-ajax-failure="error"
                  data-ajax-method="POST">
                <input type="hidden" id="cus_id" name="id" value="@Model.ProductId" />
                <label>Name</label>
                <input type="text" id="name" class="form-control" asp-for="Name" value="@Model.Name" />
                <span asp-validation-for="Name" class="text-danger"> </span>
                <br />
                <label>Price</label>
                <input type="number" min="0" id="fname" class="form-control" asp-for="price" value="@Model.Price" />
                <span asp-validation-for="Price" class="text-danger"> </span>
                <br />
                <label>Description</label>
                <input type="text" id="phone" class="form-control" asp-for="Descritpion" value="@Model.Description" />
                <span asp-validation-for="Description" class="text-danger"> </span>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.js"> </script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"> </script>

here is model class

    {
        public int ProductId { get; set; }
        [Required(ErrorMessage = "The Name is Required")]

        //[Remote("validateName", "product", ErrorMessage = "This Name is Already Exisit")]
        [Remote("ValidateCode", "product", ErrorMessage = "Please enter a valid code")]
        public string Name { get; set; }
        [Required(ErrorMessage = "The price is Required")]

        public decimal Price { get; set; }
        [Required(ErrorMessage = "The Description is Required")]
        public string Description { get; set; }

    }```

Solution

  • Yes I found the issue there are my tag Helper was not working because my shared folder and _viewImport files are in pages but I' m using the razor pages from the View folder.