【发布时间】:2022-01-04 21:57:46
【问题描述】:
我有一个字符串类型的属性,称为地址。我有第二个 GUID 类型的属性,称为 ProjectTypeId。如果 ProjectTypeId 等于某个硬编码的 GUID 值,我想将 Address 属性设为必需。如何实施此解决方案?
我尝试了以下方法,但新 Guid("123-456-7890") 出现编译器错误 CS0182 错误:
[MaxLength(10)]
[TextInput(MaxLength = 10)]
[Display(Name = "Address")]
[RequiredIf("ProjectTypeId", new Guid("123-456-7890"),
ErrorMessage = "Address is required for Commercial Project Types.")]
[StringLength(10, ErrorMessage = "The Address must be between 1 and 10 characters.", MinimumLength = 1)]
public string Address { get; set; }
注意:我在此示例中更改了实际的 GUID 值。实际的 GUID 值是其他值。
【问题讨论】:
-
RequiredIf属性是自己建的属性吗?如果是,请分享RequiredIfAttribute课程。谢谢。 -
公共类RequiredIfAttribute:ConditionalValidationAttribute {受保护的覆盖字符串ValidationName {get {返回“requiredif”; } } public RequiredIfAttribute(stringdependentProperty, object targetValue) : base(new RequiredAttribute(),dependentProperty, targetValue) { } protected override IDictionary
GetExtraValidationParameters() { return new Dictionary { { "rule" , “必需的” } }; } }
标签: c# asp.net-mvc compiler-errors guid requiredfieldvalidator