我在此处看不到验证消息。我做错的地方我看不到它在服务器端工作,我可以在后端验证但我无法向用户显示这些验证消息
这是我的模态
[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>
答案 0 :(得分:0)
我认为配置可能有问题。我创建了一个新的 asp.net core 3.1 mvc 项目。我创建了一个新的控制器、Create.cshtml 和类别模型。但我认为它可能来自标签助手。请检查您的 @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>
模型分类.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; }
}
}
测试结果: