asp.net 核心 select 数据注释验证无效

asp.net core select validation with data annotation not working

我花了很多时间来找出答案,但我做不到。当我 post 我的模型没有 select 任何选项时 - 实际上 selecting 0 - Select - 选项 Required 验证不起作用。

我还尝试从服务代码中删除以编程方式添加的默认选项到视图代码,验证也没有像这样工作。

如果我完全删除了默认选项,那么查看引擎会自动select列表中的第一个选项并且永远不会完成验证。

如何正确完成服务器端验证?

这是我的模型

public class AuditViewModel
{
    public Guid Id { get; set; }

    [Display(Name = "Subject")]
    [Required(ErrorMessage = "IsRequired")]
    public string Subject { get; set; }

    public string AuditType { get; set; }

    public string LocationCountry { get; set; }

    public string LocationOffice { get; set; }

    [Required(ErrorMessage = "IsRequired")]
    [Display(Name = "AuditType")]
    public int AuditTypeId { get; set; }

    public string CreatedOn { get; set; }

    public string ModifiedOn { get; set; }

    public string CreatedBy { get; set; }

    [Display(Name = "Description")]
    [Required(ErrorMessage = "IsRequired")]
    public string Description { get; set; }

    [Display(Name = "Country")]
    [Required(ErrorMessage = "IsRequired")]
    public int LocationCountryId { get; set; }

    [Display(Name = "Office")]
    [Required(ErrorMessage = "IsRequired")]
    public int LocationOfficeId { get; set; }

    [Display(Name = "Season")]
    public string Season { get; set; }

    public List<SelectListItem> Countries { get; set; }

    public List<SelectListItem> AuditTypes { get; set; }

    public List<SelectListItem> Offices { get; set; }

    public List<AuditViewModel> AuditList { get; set; }
}

这是获取 select 列表数据并创建绑定列表的 服务

public class AuditViewModelService : IAuditViewModelService
{
    public List<SelectListItem> GetAuditTypes()
    {
        var list = new List<SelectListItem>
            {
                new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true }
            };
        foreach (AuditType item in Enum.GetValues(typeof(AuditType)))
        {
            list.Add(new SelectListItem { Text = _enumLocalizer.GetLocalizedString(item.ToString()), Value = ((int)item).ToString() });
        }
        return list;
    }

    public List<SelectListItem> GetCountries()
    {
        var list = new List<SelectListItem> { new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true } };
        list.AddRange(_countryRepository.GetAll().ToList().ToSelectListItemList("Name"));
        return list;
    }

    public List<SelectListItem> GetOffices()
    {
        var list = new List<SelectListItem> { new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true } };
        list.AddRange(_officeRepository.GetAll().ToList().ToSelectListItemList("Name"));
        return list;
    }
}

这是 select 输入 视图的一部分

<div class="form-group m-form__group">
    <label asp-for="AuditTypeId"></label>
    <select asp-for="AuditTypeId" asp-items="@Model.AuditTypes" class="form-control m-input m-input--square" id="auditTypeId">
        <option value="0">Select</option>
    </select>
    <span asp-validation-for="AuditTypeId"></span>
</div>
<div class="form-group m-form__group">
    <label asp-for="LocationCountryId"></label>
    <select asp-for="LocationCountryId" asp-items="@Model.Countries" class="form-control m-input m-input--square" id="locationCountryId">
        <option value="0">Select</option>
    </select>
    <span asp-validation-for="LocationCountryId"></span>
</div>
<div class="form-group m-form__group">
    <label asp-for="LocationOfficeId"></label>
    <select asp-for="LocationOfficeId" asp-items="@Model.Offices" class="form-control m-input m-input--square" id="locationOfficeId">
        <option value="0">Select</option>
    </select>
    <span asp-validation-for="LocationOfficeId"></span>
</div>

来自 RequiredAttribute

The RequiredAttribute attribute specifies that when a field on a form is validated, the field must contain a value. A validation exception is raised if the property is null, contains an empty string (""), or contains only white-space characters.

现在看看你的模型属性

public int LocationCountryId { get; set; }

public int AuditTypeId { get; set; }

public int LocationOfficeId { get; set; }

int 不能为 null,不能包含空字符串,也不能取 white-space 因此在此上下文中的 Required 将每次都通过验证。 default(int) 将 return 0 因此您会注意到这些属性在回发时 0

您需要将这些更改为 int? 以便您的 属性 可以处于空状态,或者您需要使用 Range 属性并执行类似 Range(1, int.MaxValue) 这样您就可以将错误消息的值设为 0。