【问题标题】:asp-validation-for triggering on get .net coreasp-validation-for 在获取 .net 核心上触发
【发布时间】:2017-04-26 17:10:31
【问题描述】:

我有一个 .NET Core 应用程序,一旦从 get 返回页面,就会触发验证。我有一个视图模型,但我不确定为什么收到响应后就已经触发了验证。

这是我的模型和标记

    using System.ComponentModel.DataAnnotations;

    namespace MusicianProject.Models.ViewModels
    {
        public class RegisterViewModel
        {
            [Required(ErrorMessage ="First name is required.")]
            [Display(Name ="First Name")]
            [StringLength(25, ErrorMessage = "First name must be less than 25 characters.")]
            public string FirstName { get; set; }

            [Required(ErrorMessage = "Last name is required")]
            [Display(Name = "Last Name")]
            [StringLength(50, ErrorMessage = "Last name must be less than 50 characters.")]
            public string LastName { get; set; }

            [Required]
            [EmailAddress]
            [Display(Name = "Email")]
            public string Email { get; set; }

            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }

        }
    }

这是返回的寄存器视图。

@model MusicianProject.Models.ViewModels.RegisterViewModel

<div class="container">
    <form asp-controller="Account" asp-action="Register" method="post">

        <div class="col-md-4 col-md-offset-4">

            <div class="form-group">
                <label asp-for="FirstName"></label>
                <input class="form-control" type="text" asp-for="FirstName" />
                <span asp-validation-for="FirstName">First name is required.</span>
            </div>


            <div class="form-group">
                <label asp-for="LastName"></label>
                <input class="form-control" asp-for="LastName" />
                <span asp-validation-for="LastName">Last name is required.</span>
            </div>

            <div class="form-group">
                <label asp-for="Email"></label>
                <input class="form-control" type="text" asp-for="Email" />
                <span asp-validation-for="Email">Email is required.</span>
            </div>

            <div class="form-group">
                <label asp-for="Password"></label>
                <input asp-for="Password" type="password" id="password" class="form-control" />
                <span asp-validation-for="Password">Password is required.</span>
            </div>

            <div class="form-group">
                <label asp-for="ConfirmPassword"></label>
                <input asp-for="ConfirmPassword" type="password" id="confirm-password" class="form-control" />
                <span asp-validation-for="ConfirmPassword">Confirm password is required</span>
            </div>


            <div class="btn-group text-center">
                <button class="btn btn-default">Sign up!</button>
                <button class="btn btn-danger">Cancel</button>
            </div>
        </div>

    </form>
</div>

我有两种注册方法,post 和 get。这是我所拥有的。

 [HttpGet]
        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Register(RegisterViewModel rvm)
        {
            if (ModelState.IsValid)
            {

                return RedirectToAction("Index", "Home");
            }

            return View();
        }

当我在浏览器中导航到路线时,这就是我得到的。

【问题讨论】:

    标签: asp.net validation asp.net-core asp.net-core-mvc


    【解决方案1】:

    只需删除视图中&lt;span asp-validation-for&gt; 标记内的所有文本。

    标签助手会在出现验证错误时为您注入文本,具体取决于您的注释设置。您现在拥有它的方式会强制打印这些消息。

    【讨论】:

    • 我尝试这样做,如果我在这些跨度上放置一个类,使它们变为红色,它们仍然显示。我正在使用引导程序,所以我给他们上的课程是文本危险的,并且仍然显示红色轮廓。有没有办法通过数据注解来控制类?
    • @destructi6n 你能否给我看一个例子,看看你做出改变后spans 的样子?
    • @destructi6n 您可以控制该类,但它需要一个扩展 ASP 的自定义标签助手。 text-danger 类应该只使文本变为红色,而不是使红色轮廓。也许这是一些自定义 CSS 规则的问题?
    • @destructi6n Here is a very similar scenario 这会给你一个很好的起点。你会做同样的事情,但对于验证标签助手。
    • @destructi6n 我认为您可能正处于想要提出新问题的地步。我唯一的猜测是您可能需要重写非异步 Process 方法。为确保您的自定义助手正在被使用,请在构造函数中指向一个断点。
    猜你喜欢
    • 2017-02-26
    • 2020-04-09
    • 2015-10-23
    • 2018-10-17
    • 2020-04-25
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多