Ghooti Farangi
Ghooti Farangi

Reputation: 20156

How can I disable client validation for some fields in asp.net MVC

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

Upvotes: 2

Views: 1161

Answers (2)

Judo
Judo

Reputation: 5247

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

Upvotes: 1

archil
archil

Reputation: 39491

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

Upvotes: 6

Related Questions