提问人:tyson 提问时间:6/30/2023 更新时间:6/30/2023 访问量:42
将输入字段值放置在另一个表单字段的数据注释参数中 asp.net
Placing input field value inside data annotation parameter for another form field asp.net
问:
我有一组单选按钮和一个文本字段。如果选择了两个单选按钮之一,我想使文本字段成为必需的。我使用数据注释制作了一个自定义验证类。我似乎无法弄清楚如何将单选按钮的值作为参数传递给数据注释。我似乎无法使用它里面的“radioButton”给出一个错误,因为它不是静态的。但是我不能让它变得静态,否则它会破坏我无法更改的其他代码。 型
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;
}
}
我尝试在数据注释中添加字段名称。我想知道是否有办法将它拉到课堂内。
答: 暂无答案
评论