必须data-val-required和data-val-number具有相同的错误消息

时间:2022-05-07 19:17:32

Must data-val-required and data-val-number have the same error messages?

必须data-val-required和data-val-number是否具有相同的错误消息?

I am just wondering as I am viewing the generated output of my HTML.

我只是想知道我正在查看HTML的生成输出。

<div class="form-group">
     @Html.LabelFor(m => m.Countries)
     <div class="row">
          <div class="col-lg-4">
               @Html.DropDownListFor(
                    m => m.CountryId,
                    new SelectList(Model.Countries, "Id", "Name", Model.CountryId),
                    "-- Select --",
                    new { @class = "form-control" }
               )
          </div>
     </div>
     @Html.ValidationMessageFor(x => x.CountryId)
</div>

My viewmodel looks like this:

我的viewmodel看起来像这样:

public class MyViewModel : ViewModelBase
{
     public MyViewModel()
     {
          Countries = Enumerable.Empty<CountryDTO>();
     }

     [Required(ErrorMessage = "Country is required")]
     public int CountryId { get; set; }

     [Display(Name = "Country")]
     public IEnumerable<CountryDTO> Countries { get; set; }
}

The generated HTML from the above looks like this:

上面生成的HTML如下所示:

<select name="CountryId" id="CountryId" data-val-required="Country is required" data-val-number="The field CountryId must be a number." data-val="true" class="form-control">
     <option value="">-- Select --</option>
     <option value="1">country 1</option>
     <option value="2">country 2</option>
     <option value="3">country 3</option>
     <option value="4">country 4</option>
</select>

Why do data-val-required and data-val-number have 2 different messages? Don't they need to be the same?

为什么data-val-required和data-val-number有2条不同的消息?他们不需要一样吗?

1 个解决方案

#1


0  

No, required is displayed when the user didn't provide any input at all, while number is displayed when the provided input isn't a number.

否,当用户根本没有提供任何输入时显示必需,而当提供的输入不是数字时显示数字。

You can see it also in a way that in the required-error message you can provide the user with information, why the field is required, while in the number-error message you can provide the user with additional information, why the field needs to be numeric.

您也可以通过以下方式查看它:在必需的错误消息中,您可以向用户提供信息,为什么需要该字段,而在数字错误消息中,您可以向用户提供其他信息,为什么字段需要是数字。

#1


0  

No, required is displayed when the user didn't provide any input at all, while number is displayed when the provided input isn't a number.

否,当用户根本没有提供任何输入时显示必需,而当提供的输入不是数字时显示数字。

You can see it also in a way that in the required-error message you can provide the user with information, why the field is required, while in the number-error message you can provide the user with additional information, why the field needs to be numeric.

您也可以通过以下方式查看它:在必需的错误消息中,您可以向用户提供信息,为什么需要该字段,而在数字错误消息中,您可以向用户提供其他信息,为什么字段需要是数字。