ASP.NET中如何将单选按钮值传入数据注解参数实现字段验证
问题描述
我有一组单选按钮和一个文本字段,希望选中特定的单选按钮选项(比如"Single Pdfs")时,文本字段变为必填项。我尝试用数据注解写了自定义验证类,但没法把单选按钮的当前值传给注解参数——直接用radioButton字段会因为非静态报错,又不能把它改成静态,怕破坏其他不能修改的代码。下面是我的Model和自定义验证类代码,想问问能不能在验证类里直接获取单选按钮的字段值?
原Model代码
public string radioButton { get => This.ArchiveFileOptions; set => SetProperty(This.ArchiveFileOptions, value, () => { This.ArchiveFileOptions = value; ObjectState = ObjectState != ObjectStateEnum.Added ? ObjectStateEnum.Modified : ObjectStateEnum.Added; }); } [IfRadioSelectedTextRequired(text: "Single Pdfs", radioValue: {{RADIO BUTTON VALUE HERE}})] public string textField { get => This.PdfIndexingRequirements; set => SetProperty(This.PdfIndexingRequirements, value, () => { This.PdfIndexingRequirements = value; ObjectState = ObjectState != ObjectStateEnum.Added ? ObjectStateEnum.Modified : ObjectStateEnum.Added; }); }
原自定义验证类代码
public class IfRadioSelectedTextRequired : ValidationAttribute { private readonly string _text; private readonly string _radioValue; public IfRadioSelectedTextRequired(string text, string radioValue) { _text = text; _radioValue = radioValue; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var fieldValue = value as string; if (_radioValue != null) { if (_radioValue == _text) { if (fieldValue == null) { //error return new ValidationResult($"This field is required"); } if (fieldValue == "") { //error return new ValidationResult($"This field is required"); } if (fieldValue != null) { //success return ValidationResult.Success; } } if (_radioValue != _text) { //success return ValidationResult.Success; } } //success return ValidationResult.Success; } }
解决方案
数据注解的构造参数必须是编译时常量,没法直接传入实例字段的动态值,所以要修改验证逻辑,在验证时从Model实例中动态获取单选按钮的当前值:
修改后的自定义验证类
public class IfRadioSelectedTextRequired : ValidationAttribute { // 需要触发必填的单选按钮目标值 private readonly string _targetRadioValue; // Model中单选按钮字段的名称 private readonly string _radioFieldName; public IfRadioSelectedTextRequired(string targetRadioValue, string radioFieldName) { _targetRadioValue = targetRadioValue; _radioFieldName = radioFieldName; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { // 获取当前验证的Model实例 var model = validationContext.ObjectInstance; // 通过反射获取单选按钮字段的属性信息 var radioProperty = model.GetType().GetProperty(_radioFieldName); if (radioProperty == null) { return new ValidationResult($"找不到指定的单选按钮字段:{_radioFieldName}"); } // 获取单选按钮的当前值 var radioValue = radioProperty.GetValue(model) as string; // 如果单选按钮选中了目标值,检查文本字段是否为空 if (radioValue == _targetRadioValue) { var textFieldValue = value as string; if (string.IsNullOrWhiteSpace(textFieldValue)) { return new ValidationResult("此字段为必填项"); } } // 验证通过 return ValidationResult.Success; } }
修改后的Model用法
public string radioButton { get => This.ArchiveFileOptions; set => SetProperty(This.ArchiveFileOptions, value, () => { This.ArchiveFileOptions = value; ObjectState = ObjectState != ObjectStateEnum.Added ? ObjectStateEnum.Modified : ObjectStateEnum.Added; }); } // 使用nameof避免硬编码字段名,更易维护 [IfRadioSelectedTextRequired(targetRadioValue: "Single Pdfs", radioFieldName: nameof(radioButton))] public string textField { get => This.PdfIndexingRequirements; set => SetProperty(This.PdfIndexingRequirements, value, () => { This.PdfIndexingRequirements = value; ObjectState = ObjectState != ObjectStateEnum.Added ? ObjectStateEnum.Modified : ObjectStateEnum.Added; }); }
说明
- 不再通过注解参数传递单选按钮的动态值,而是传入字段名称,在验证时通过反射从Model实例中获取当前值,完美避开静态字段的限制。
- 使用
nameof(radioButton)替代硬编码字段名,后续字段名修改时编译器会自动提示,避免出错。 - 用
string.IsNullOrWhiteSpace统一判断空值和空字符串,简化逻辑。
内容的提问来源于stack exchange,提问作者tyson
相关产品推荐
相关产品推荐

