.net核心jquery验证显示错误消息

wmomyfyw  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(541)

我在这里看不到验证消息。在我出错的地方,我看不到它在服务器端工作,我可以在后端进行验证,但我无法向用户显示这些验证消息

这是我的情态

[DisplayName("Kategori Adı")]
    [Required(ErrorMessage ="{0} boş geçilmemelidir.")]
    [MaxLength(50,ErrorMessage ="{0} {1} karakterden büyük olamaz.")]
    [MinLength(3, ErrorMessage = "{0} en az {1} karakterden oluşmalıdır.")]
    public string CategoryName { get; set; }
    [DisplayName("Kategori Açıklama")]
    [MaxLength(50, ErrorMessage = "{0} {1} karakterden büyük olamaz.")]
    [MinLength(3, ErrorMessage = "{0} en az {1} karakterden oluşmalıdır.")]
    public string CategoryDesc { get; set; }

这是我的看法

@model Mvc.Entities.Dtos.CategoryAddDto
<head>
    <script src="https://code.jquery.com/jquery.js" crossorigin="anonymous"></script>
    <script src="~/jquery-validate/jquery.validate.js"></script>
    <script src="~/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

</head>
    <div class="modal-body">
                        <form action="Add" id="form-category-add" method="post">
                            <input type="text" id="isvalid" value="False" />
                            <div asp-validation-summary="All" class="text-danger" id="validation-summary" aria-hidden="true">

                            </div>
                            <div class="form-group">
                                <label asp-for="CategoryName"></label>
                                <input type="text" name="CategoryName" class="form-control" asp-for="CategoryName" value="" />
                                <label class="text-danger" asp-validation-for="CategoryName"></label>
                            </div>

                            <div class="form-group">
                                <label asp-for="CategoryDesc"></label>
                                <input type="text" name="CategoryDesc" class="form-control" asp-for="CategoryDesc" value="" />
                                <label class="text-danger" asp-validation-for="CategoryDesc"></label>
                            </div>
                        </form>
                    </div>
sc4hvdpw

sc4hvdpw1#

我认为配置可能有问题。我创建了一个新的asp.net core 3.1 mvc项目。我创建了一个新的控制器create.cshtml和category模型。但我认为它可能来自标签助手。请检查是否有 @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 在你的 _ViewImports.cshtml
控制器:

using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HelloController : Controller
    {
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create([Bind("CategoryName,CategoryDesc")] Category cate)
        {
            return View(cate);
        }
    }
}

我的视图和模型基本上是从您的代码片段复制的。create.cshtml我使用了cdn,但基本上,这些脚本包含在 _ValidationScriptsPartial.cshtml

@model WebApplication1.Models.Category

<head>
    <script src="https://code.jquery.com/jquery.js" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.min.js"></script>

</head>

<div class="modal-body">
    <form action="Create" id="form-category-add" method="post">
        <input type="text" id="isvalid" value="False" />
        <div asp-validation-summary="All" class="text-danger" id="validation-summary" aria-hidden="true">

        </div>
        <div class="form-group">
            <label asp-for="CategoryName"></label>
            <input type="text" name="CategoryName" class="form-control" asp-for="CategoryName" value="" />
            <label class="text-danger" asp-validation-for="CategoryName"></label>
        </div>

        <div class="form-group">
            <label asp-for="CategoryDesc"></label>
            <input type="text" name="CategoryDesc" class="form-control" asp-for="CategoryDesc" value="" />
            <label class="text-danger" asp-validation-for="CategoryDesc"></label>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>
</div>

型号category.cs:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Models
{
    public class Category
    {
        [DisplayName("Kategori Adı")]
        [Required(ErrorMessage = "{0} boş geçilmemelidir.")]
        [MaxLength(50, ErrorMessage = "{0} {1} karakterden büyük olamaz.")]
        [MinLength(3, ErrorMessage = "{0} en az {1} karakterden oluşmalıdır.")]
        public string CategoryName { get; set; }
        [DisplayName("Kategori Açıklama")]
        [MaxLength(50, ErrorMessage = "{0} {1} karakterden büyük olamaz.")]
        [MinLength(3, ErrorMessage = "{0} en az {1} karakterden oluşmalıdır.")]
        public string CategoryDesc { get; set; }
    }
}

测试结果:

相关问题