<bdo id='FkAYo'></bdo><ul id='FkAYo'></ul>

<tfoot id='FkAYo'></tfoot>

    <i id='FkAYo'><tr id='FkAYo'><dt id='FkAYo'><q id='FkAYo'><span id='FkAYo'><b id='FkAYo'><form id='FkAYo'><ins id='FkAYo'></ins><ul id='FkAYo'></ul><sub id='FkAYo'></sub></form><legend id='FkAYo'></legend><bdo id='FkAYo'><pre id='FkAYo'><center id='FkAYo'></center></pre></bdo></b><th id='FkAYo'></th></span></q></dt></tr></i><div id='FkAYo'><tfoot id='FkAYo'></tfoot><dl id='FkAYo'><fieldset id='FkAYo'></fieldset></dl></div>
    <legend id='FkAYo'><style id='FkAYo'><dir id='FkAYo'><q id='FkAYo'></q></dir></style></legend>

      1. <small id='FkAYo'></small><noframes id='FkAYo'>

        MVC .Net Core 模型验证 - 值“"无效.错误

        时间:2023-07-11

      2. <tfoot id='3Cc14'></tfoot>

                <tbody id='3Cc14'></tbody>

            1. <legend id='3Cc14'><style id='3Cc14'><dir id='3Cc14'><q id='3Cc14'></q></dir></style></legend>

              <small id='3Cc14'></small><noframes id='3Cc14'>

              <i id='3Cc14'><tr id='3Cc14'><dt id='3Cc14'><q id='3Cc14'><span id='3Cc14'><b id='3Cc14'><form id='3Cc14'><ins id='3Cc14'></ins><ul id='3Cc14'></ul><sub id='3Cc14'></sub></form><legend id='3Cc14'></legend><bdo id='3Cc14'><pre id='3Cc14'><center id='3Cc14'></center></pre></bdo></b><th id='3Cc14'></th></span></q></dt></tr></i><div id='3Cc14'><tfoot id='3Cc14'></tfoot><dl id='3Cc14'><fieldset id='3Cc14'></fieldset></dl></div>
              • <bdo id='3Cc14'></bdo><ul id='3Cc14'></ul>
                • 本文介绍了MVC .Net Core 模型验证 - 值“"无效.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 MVC .Net Core 中使用模型验证,但无法替换此默认错误消息值"无效".

                  I am trying to use Model Validation in MVC .Net Core and can't manage to replace this default error message 'The value '' is invalid'.

                  理论上,我们可以通过在Model中使用ErrorMessage Annotation来替换我们自己自定义的错误信息.但我找不到一种方法来实现这一点.

                  In theory, we can replace our own custom error message by using ErrorMessage Annotation in the Model. But I couldn't find a way to make this one work.

                  我的模型

                  [Required(ErrorMessage = "Date Required")]
                  [DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]                
                  [Display(Name = "Appointment Date")]
                  [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
                  public DateTime AppointmentDate { get; set; }
                  

                  如上所示,我为 RequiredDataType 标签放置了不同的 ErrorMessage.

                  I put different ErrorMessage for both Required and DataType tag as shown in the above.

                  我的 html 视图

                      <div class="col-md-2">
                          <input class="form-control" asp-for="AppointmentDate">
                          <span asp-validation-for="AppointmentDate" class="text-danger"></span>
                      </div>
                  

                  能否请您帮我如何替换该错误消息?谢谢.

                  Could you please help me how I could get that error message replaced? Thanks.

                  推荐答案

                  为了使您的 Required 属性起作用,您需要使字段可为空:

                  In order to make your Required attribute works you need to make field nullable:

                  public DateTime? AppointmentDate { get; set; }
                  

                  Edit:还请注意,DataType 属性实际上不对字段执行验证.将 post 数据绑定到 model

                  Edit: also note that DataType attribute actually doesn't perform validation on field. MVC validate date when applying binding from post data to model

                  这篇关于MVC .Net Core 模型验证 - 值“"无效.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何注册自定义 UserStore &amp;DI 中的用户管理器 下一篇:如何在 ASP.net Core 中添加 js 和 css 文件?

                  相关文章

                • <small id='Wq3rP'></small><noframes id='Wq3rP'>

                    <legend id='Wq3rP'><style id='Wq3rP'><dir id='Wq3rP'><q id='Wq3rP'></q></dir></style></legend>

                    <i id='Wq3rP'><tr id='Wq3rP'><dt id='Wq3rP'><q id='Wq3rP'><span id='Wq3rP'><b id='Wq3rP'><form id='Wq3rP'><ins id='Wq3rP'></ins><ul id='Wq3rP'></ul><sub id='Wq3rP'></sub></form><legend id='Wq3rP'></legend><bdo id='Wq3rP'><pre id='Wq3rP'><center id='Wq3rP'></center></pre></bdo></b><th id='Wq3rP'></th></span></q></dt></tr></i><div id='Wq3rP'><tfoot id='Wq3rP'></tfoot><dl id='Wq3rP'><fieldset id='Wq3rP'></fieldset></dl></div>
                    <tfoot id='Wq3rP'></tfoot>

                      <bdo id='Wq3rP'></bdo><ul id='Wq3rP'></ul>