0

In my model I have the below property

public int? Data
{
    get;
    set;
}

and using @Html.TextBoxFor(m => m.Data) I am adding it in the view. I have another property in the model

public DateTime? DateData { get; set; }

which I display using @Html.FormTextBoxFor(m => m.DateData, new { id = "DateData " })

How can I use the RequiredIf attribute or any other method to make the DateData required if the value of Data is not 0. So anytime the value of Data is not zero or null I want the DateData to be a required property.

2
  • 1
    To the best of my knowledge RequiredIf is not something that is provided by vanilla asp.net-mvc requiring one to use a third party / custom implementation. It would be helpful if you could provide a link to the RequiredIf implementation you chose. Commented Jul 6, 2015 at 16:53
  • So apart from RequiredIf is there anything else built into MVC for this. Commented Jul 6, 2015 at 17:01

1 Answer 1

1

Add another property

public bool DataIsNotZero 
{
   get
   {
       return Data !=0 || Data !=null ;
   }
}

Then using RequiredIf

[RequiredIf("DataIsNotZero", true, ErrorMessage = "Required!!!")]
public DateTime? DateData { get; set; }

Please note:

As pointed out in the comments, Required if is not a built-in attribute. You need to download and import third party library:

MVC Foolproof Validation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.