How to use RequiredIf with a property of GUID type?

150 Views Asked by At

I have a property of string type called Address. I have a second property of GUID type called ProjectTypeId. I want to make the Address property required if ProjectTypeId equals a certain hardcoded GUID value. How can I implement this solution?

I tried the following, but compiler error CS0182 error is presented for new Guid("123-456-7890"):

    [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; } 

Note: I changed the actual GUID value in this example. The actual GUID value is something else.

1

There are 1 best solutions below

0
AudioBubble On

The following URL resource provides the solution that I needed: https://www.c-sharpcorner.com/article/dotvvm-build-conditional-validation-attribute/

I updated my code to be:

[RequiredIf("ProjectTypeId", "123-456-7890", ErrorMessage = "Address is...")]

Note: I changed the actual GUID value in this example. The actual GUID value is something else.