2

How can I disable client validation for some fields in asp.net MVC. I have not defined any validation for Order property but it show "The Order field is required" in client. I want to disable client validation for Order.

   public class Product
    {
     ...
     [Required(ErrorMessage = "Name Is Required")]
     public String Name { get; set; }

     public int Order { get; set; }
     ...
    }

2 Answers 2

6

int is non nullable type. Is cannot be assigned empty value, so metadata provider automatically makes it required. If your business doesn't need Order field filled in for Product class, make it nullable type - Nullable<int> Order. This way it won't be made required unless you explicitly make it. Well, there is another way - using some scripts on client side. But this will disable validation only on client side, after post to server, the error message will still appear unless you make it nullable. Assuming you use default jquery validation plugin for asp.net mvc 3, you can use remove rules function

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

Comments

1

The simplest way I found around this is to use the ? character to set the value type (in this case int) to a nullable value type:

public int? Order { get; set; }

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.