带有数据注释的asp.net核心选择验证不起作用

ibu*_*ubi 2 validation select data-annotations razor asp.net-core-mvc

我花了很多时间来找出答案,但我没有。当我发布模型而未选择任何选项(实际上是选择0 - Select-option)时Required验证不起作用。

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

如果我完全删除了默认选项,则视图引擎会自动从列表中选择第一个选项,并且永远不会完成验证。

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

这是我的模特

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; }
}
Run Code Online (Sandbox Code Playgroud)

这是服务是获得选择列表数据,并结合创建列表

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是视图的选择输入部分

<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>
Run Code Online (Sandbox Code Playgroud)

Sho*_*hoe 5

RequiredAttribute

RequiredAttribute属性指定在验证表单上的字段时,该字段必须包含一个值。如果属性为null,包含空字符串(“”)或仅包含空格字符,则会引发验证异常。

现在查看您的模型属性

public int LocationCountryId { get; set; }

public int AuditTypeId { get; set; }

public int LocationOfficeId { get; set; }

int不能为null,不能包含空字符串并且不能使用空格,因此Required在这种情况下,每次都会通过验证。default(int)将返回,0因此您将注意到这些属性处于0回发状态。

您需要将其更改为,int?以便您的属性可以为空状态,或者您需要使用该Range属性并执行类似的操作,Range(1, int.MaxValue)以便可以将错误消息的目标值设为0。