如果我没记错的话,只能将原始类型作为参数类型传递给定制的<?php
if(isset($_POST['formSubmit'])) {
echo $_POST['name'];
}
?>
(例如ValidationAttribute
):
string
并像这样使用它:
public class AttributeNameValidation : ValidationAttribute
{
public string AttributeName { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (MyProperty == value.ToString())
{
return new ValidationResult($"Attribute with the name '{value.ToString()}' already exist.", new[] { validationContext.MemberName });
}
return null;
}
}
但是,如果我们想将非基本类型传递给定制的[Required]
[AttributeNameValidation(AttributeName = "MyAttribute")]
public string Name { get; set; }
怎么办?例如,如何将这个对象的实例传递到ValidationAttribute
中? ( 同时保持外部属性为非静态 )
AttributeNameValidation
假设在public class Attribute
{
public string Name { get; set; }
public string Type { get; set; }
}
public Attribute myAttrib = new Attribute {Name = "Price", Type = "int"};
中创建了名称为Attribute
(AttributeNameValidation
)的类型Attrib
的属性,则IntelliSense会抱怨:
public Attribute Attrib { get; set; }
答案 0 :(得分:0)
这不是Blazor问题,而是属性restriction。
因此,您需要在属性中添加多个属性:
public class AttributeNameValidation : ValidationAttribute
{
public string AttributeName { get; set; }
public string AttriuteType {get;set;}
}
如果需要始终同时设置它们,请使用构造函数:
public class AttributeNameValidation : ValidationAttribute
{
public string AttributeName { get;}
public string AttriuteType {get;}
AttributeNameValidation(string attrName, string attrType)
{
AttributeName = attrName;
AttrType = attrType;
}
}