C# 如何使用FluentValidation获取单选按钮所需的错误消息
我使用的是C# 如何使用FluentValidation获取单选按钮所需的错误消息,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,fluentvalidation,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Fluentvalidation,我使用的是ASP.NETMVC4和最新的FluentValidation 我正在努力让我的单选按钮进行验证。当我点击提交按钮时,我需要在单选按钮列表中进行选择 我认为我有以下几点: @model MyProject.ViewModels.Servers.ServicesViewModel @Html.ValidationMessageFor(x => x.ComponentTypeId) @foreach (var componentType in Model.ComponentTypes
ASP.NETMVC4
和最新的FluentValidation
我正在努力让我的单选按钮进行验证。当我点击提交按钮时,我需要在单选按钮列表中进行选择
我认为我有以下几点:
@model MyProject.ViewModels.Servers.ServicesViewModel
@Html.ValidationMessageFor(x => x.ComponentTypeId)
@foreach (var componentType in Model.ComponentTypes)
{
<div>
@Html.RadioButtonFor(x => x.ComponentTypeId, componentType.Id, new { id = "emp" + componentType.Id })
@Html.Label("emp" + componentType.Id, componentType.Name)
</div>
}
我的操作方法的一部分,其中我设置了视图模型的属性:
ServicesViewModel viewModel = new ServicesViewModel
{
ComponentTypes = componentTypeRepository.FindAll(),
Domains = domainRepository.FindAll()
};
[Validator(typeof(ServicesViewModelValidator))]
public class ServicesViewModel
{
public int ComponentTypeId { get; set; }
public IEnumerable<ComponentType> ComponentTypes { get; set; }
public int DomainId { get; set; }
public IEnumerable<Domain> Domains { get; set; }
}
我的视图模型:
ServicesViewModel viewModel = new ServicesViewModel
{
ComponentTypes = componentTypeRepository.FindAll(),
Domains = domainRepository.FindAll()
};
[Validator(typeof(ServicesViewModelValidator))]
public class ServicesViewModel
{
public int ComponentTypeId { get; set; }
public IEnumerable<ComponentType> ComponentTypes { get; set; }
public int DomainId { get; set; }
public IEnumerable<Domain> Domains { get; set; }
}
当未选择任何内容时,如何让它显示我所需的消息?我认为问题在于您对
ComponentId
使用的是int
,而不是可为空的int
。然后您将使用NotNull()
验证程序,该验证程序将永远不会触发,因为int
不能为null
尝试将其切换到以下位置:
[Validator(typeof(ServicesViewModelValidator))]
public class ServicesViewModel
{
public int? ComponentTypeId { get; set; }
public IEnumerable<ComponentType> ComponentTypes { get; set; }
public int DomainId { get; set; }
public IEnumerable<Domain> Domains { get; set; }
}
[验证器(类型(ServicesViewModelValidator))]
公共类服务可视模型
{
public int?ComponentTypeId{get;set;}
公共IEnumerable组件类型{get;set;}
公共int域ID{get;set;}
公共IEnumerable域{get;set;}
}
如果该剂量无效,则您可以尝试使用范围验证:
public class ServicesViewModelValidator : AbstractValidator<ServicesViewModel>
{
public ServicesViewModelValidator()
{
RuleFor(x => x.ComponentTypeId)
.InclusiveBetween(1, int.MaxValue)
.WithMessage("Required");
RuleFor(x => x.DomainId)
.NotNull()
.WithMessage("Required");
}
}
公共类服务viewmodelvalidator:AbstractValidator
{
公共服务VIEWMODELVALIDATOR()
{
RuleFor(x=>x.ComponentTypeId)
.inclusivebeween(1,int.MaxValue)
.WithMessage(“必需”);
RuleFor(x=>x.DomainId)
.NotNull()
.WithMessage(“必需”);
}
}
这将导致0不是有效值
public class ServicesViewModelValidator : AbstractValidator<ServicesViewModel>
{
public ServicesViewModelValidator()
{
RuleFor(x => x.ComponentTypeId)
.InclusiveBetween(1, int.MaxValue)
.WithMessage("Required");
RuleFor(x => x.DomainId)
.NotNull()
.WithMessage("Required");
}
}