简体   繁体   English

使用数据注释验证绑定到 SelectList 对象的下拉列表

[英]Validating a dropdown bound to a SelectList object using data annotations

I'm trying to follow best practices for validations in ASP.NET Core.我正在尝试遵循 ASP.NET Core 中的验证最佳实践。 I have a contact form with the page bound to a view model.我有一个联系表单,页面绑定到视图模型。

Model模型

public class ViewModel {
    ...
    [Required(ErrorMessage = "Select a city")]
    public int CityID { get; set; }
    public SelectList CityList { get; set; }
}

View看法

<select id="input-request-type" class="form-control" asp-for="CityID" asp-items="@Model.CityList">
    <option value="" hidden disabled selected>Select a city</option>
</select>
<span asp-validation-for="CityID"></span>

This converts to something like this in HTML:这将转换为 HTML 中的类似内容:

<select class="input-validation-error" data-val="true" data-val-required="Select a city" name="CityID">
    <option value="" hidden="" disabled="" selected="">Select a city</option>
    <option value="9">Toronto</option>
    <option value="12">New York</option>
</select>

The problem with the above code is that the validation does not trigger an error.上面代码的问题是验证不会触发错误。 My guess is that the validation still detects a value from the submission even though it is a blank string.我的猜测是验证仍然会从提交中检测到一个值,即使它是一个空白字符串。

What I tried:我尝试了什么:

I tried modifying the data annotations on the model like so:我尝试像这样修改模型上的数据注释:

[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
...
[BindRequired]
public int CityID { get; set; }
public SelectList CityList { get; set; }

This shows a validation error, but it shows Name is required .这显示了一个验证错误,但它显示Name is required It picks up the first validation error available, but not quite what I wanted, so I changed it to the following:它选择了第一个可用的验证错误,但不是我想要的,所以我将其更改为以下内容:

...
[BindRequired]
[Required(ErrorMessage = "Select a city")]
public int CityID { get; set; }
public SelectList CityList { get; set; }

This also shows an error but it completely ignores the error message I want.这也显示了一个错误,但它完全忽略了我想要的错误消息。 The output is A value for the 'CityID' property was not provided.输出是A value for the 'CityID' property was not provided.

Any idea on how to properly show the error message I want?关于如何正确显示我想要的错误消息的任何想法? I can't seem to find any documentation on this problem.我似乎找不到有关此问题的任何文档。

It seems you can override the message on the view itself. 看来你可以覆盖视图本身的消息。

Model 模型

[BindRequired]
public int CityID { get; set; }
public SelectList CityList { get; set; }

View 视图

<style>
    .field-validation-valid {
        display: none;
    }
</style>

<select id="input-request-type" class="form-control" asp-for="CityID" asp-items="@Model.CityList">
    <option value="" hidden disabled selected>Select a city</option>
</select>
<span asp-validation-for="CityID">Select a city</span>

The span element for the validation transforms into the following depending if the value is valid or not: 验证的span元素将转换为以下内容,具体取决于值是否有效:

// valid
<span class="field-validation-valid" data-valmsg-for="CityID" data-valmsg-replace="true">
    Select a city
</span>

// invalid
<span class="field-validation-error text-danger" data-valmsg-for="CityID" data-valmsg-replace="true">
    Select a city
</span>

在 ViewModel 类中,在 CityID 属性之前添加此注释:

[Range(1, int.MaxValue, ErrorMessage = "This field is required!")]

You can achieve this by combining nullable property definition and Required attribute:您可以通过组合可空属性定义和必需属性来实现此目的:

[Required(ErrorMessage = "City is Required.")]
public int? CityID { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM