如何删除现有数据注释评估并添加动态验证 [英] How to remove existing dataannotaion valuation and add dynamic validation
问题描述
想要删除常规快递验证只附加必要的验证
i我无法删除常规快递验证
Wanted to remove regular express validation only append required validation
i am not able remove regular express validation
public class HomeController : Controller
{
public ActionResult Index()
{
ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
return View();
}
[HttpPost]
public ActionResult Index(Person objPerson)
{
if (ModelState.IsValid)
{
}
return View(objPerson);
}
}
public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
{
protected override IEnumerable<modelvalidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<attribute> attributes)
{
if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "FirstName")
{
attributes = new List<attribute>() { new RequiredAttribute() };
}
return base.GetValidators(metadata, context, attributes);
}
}
public class Person
{
[RegularExpression(@"^[a-zA-Z0-9\-\s]*$")]
public string FirstName { get; set; }
}
只是我想删除正则表达式validatin检查只需要添加所需的验证
我尝试了什么:
DataAnnotationsModelValidatorProvider得到了添加动态验证的方法运行时间
just i want to remove regular expression validatin check only need to add required validation
What I have tried:
DataAnnotationsModelValidatorProvider got way to add dynamic validation on run time
推荐答案
)]
public string FirstName { get ; set ;}
}
")] public string FirstName { get; set; } }
只是我想删除正则表达式validatin检查只需添加所需的验证
我尝试过:
DataAnnotationsModelValidatorProvider得到了在运行时添加动态验证的方法
just i want to remove regular expression validatin check only need to add required validation
What I have tried:
DataAnnotationsModelValidatorProvider got way to add dynamic validation on run time
这篇关于如何删除现有数据注释评估并添加动态验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!