使用 Fluent Validation 时看不到错误验证消息

I can't see error validation messages when using Fluent Validation

提问人:gkmnada 提问时间:8/21/2023 更新时间:8/21/2023 访问量:34

问:

我在我的项目中使用 Fluent Validation。我发出警告,以便列不应为空。但是屏幕上没有警告,我哪里出错了?

任务列表验证器.cs,

public class TaskListValidator : AbstractValidator<TaskList>
    {
        public TaskListValidator()
        {
            RuleFor(x => x.TaskTitle).NotEmpty().WithMessage("Konu Başlığı Alanı Boş Bırakılamaz");
            RuleFor(x => x.TaskDetails).NotEmpty().WithMessage("Açıklama Alanı Boş Bırakılamaz");
            RuleFor(x => x.Priority).NotEmpty().WithMessage("Lütfen Konunun Öncelik Türünü Seçiniz");
        }
    }

Index.cshtml,

<form method="post">
    <div id="divAddTask" class="card" style="display:none">
        <div class="card-body">
            <div class="card-body p-4">
                <h5 class="card-title fw-semibold mb-4">Yeni İş Oluştur</h5>
                <div class="table-responsive">
                    <label class="form-label">Konu Başlığı</label>
                    <input id="txtAddTaskTitle" class="form-control" name="TaskTitle" type="text">
                    @Html.ValidationMessageFor(x=> x.TaskTitle)
                    <br />
                    <label class="form-label">Açıklama</label>
                    <textarea id="txtAddTaskDetails" class="form-control" name="TaskDetails" rows="10"></textarea>
                    @Html.ValidationMessageFor(x=> x.TaskDetails)
                    <br />
                    <label class="form-label">Öncelik</label>
                    <select class="form-control" name="Priority" id="selectPriority">
                        <option value="0">Lütfen Öncelik Durumunu Seçiniz</option>
                        <option value="1">Düşük</option>
                        <option value="2">Normal</option>
                        <option value="3">Yüksek</option>
                    </select>
                    @Html.ValidationMessageFor(x=> x.Priority)
                    <br />
                    <button type="button" class="btn mb-1 waves-effect waves-light btn-light-primary text-primary" id="btnAddTask">Oluştur</button>
                    <button type="button" class="btn mb-1 waves-effect waves-light btn-light-warning text-warning" id="btnGetList">Listeye Dön</button>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
        $("#btnAddTask").click(function () {
            let addValue = {
                TaskTitle: $("#txtAddTaskTitle").val(),
                TaskDetails: $("#txtAddTaskDetails").val(),
                Priority: $("#selectPriority").val()
            }
            $.ajax({
                type: "Post",
                url: "/TaskList/AddTask/",
                data: addValue,
                success: function (fn) {
                    let result = jQuery.parseJSON(fn);
                    successalert();
                    let divAddTask = document.getElementById("divAddTask");
                    divAddTask.style.display = "none";
                    let divGetList = document.getElementById("divGetList");
                    divGetList.style.display = "block";
                }
            });
        });
    </script>

任务列表控制器 .cs,

[HttpPost]
        public IActionResult AddTask(TaskList taskList)
        {
            TaskListValidator taskListValidator = new TaskListValidator();
            ValidationResult validationResult = taskListValidator.Validate(taskList);
            if (validationResult.IsValid)
            {
                taskList.CreateDate = DateTime.Now;
                taskList.FinishDate = DateTime.Now;
                taskList.CreateUser = 1;
                taskList.Status = 1;
                taskListManager.itemAdd(taskList);
                var addValues = JsonConvert.SerializeObject(taskList);
                return Json(addValues);
            }
            else
            {
                foreach (var item in validationResult.Errors)
                {
                    ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
                }
            }
            return View();
        }

我以为这是一个问题,因为我正在使用 Ajax 执行插入操作,但是当我执行正常的插入操作时,错误验证消息没有出现。

C# jQuery ASP.NET 核心 验证 FluentValidation

评论

0赞 Xinran Shen 8/21/2023
Fluent Validation 是一个服务器端库,不直接提供任何客户端验证。我更喜欢在客户端而不是服务器端使用 ajax。相反,您可以使用 FormHelper 等库通过 AJAX 执行完整的服务器端规则。这允许您使用 FluentValidation(来自 Fluent 验证网站)的全部功能。我没有在您的代码中发现任何错误,这些错误导致验证错误在您提交没有ajax的表单时未显示。

答: 暂无答案