title: ASP MVC Data Annotations and database null values
body: <p>I have a asp mvc 5 project and have created the database using code first and data migrations. </p>
  
  <p>This is my current product POCO class</p>
  
  <pre><code>public class Product
  {
      [Key]
      public int ProductId { get; set; }
  
      [Required(ErrorMessage = "Please add a product name.")]
      [StringLength(50)]
      public string Name { get; set; }
      public string Code { get; set; }
  
      [DataType(DataType.MultilineText)]
      public string Description { get; set; }
      public string Features { get; set; }
      public decimal Price { get; set; }
      public bool? Promotion { get; set; }
  
      [Display(Name = "Category")]
      public int CategoryId { get; set; }
      public virtual Category Category { get; set; }
  
      [Display(Name = "Brand")]
      public int BrandId { get; set; }
      public virtual Brand Brand { get; set; }
  
      public byte[] ImageData { get; set; }
  
      [HiddenInput(DisplayValue = false)]
      public string ImageMimeType { get; set; }
  }
  </code></pre>
  
  <p>In the database the only values that are not null, is the primary key and the name. I am having a problem with the <strong>Price</strong>, <strong>Category</strong> and <strong>Brand</strong> fields. In the form all 3 are still being <em>"required"</em> and the ModelState is not passing with jqueryval.js indicating that this values need to be added. </p>
  
  <p><img src="https://i.stack.imgur.com/YgQy8.png" alt="enter image description here"></p>
  
  <p>As you can see in the image, the form will first show that the price &amp; name values need to be added. Once i add those values and click "create". I am only then asked to add values for the brand and category. As indicated in the image below.</p>
  
  <p><img src="https://i.stack.imgur.com/BYbjE.png" alt="enter image description here"></p>
  
  <p>It seems as if the brand and category and not being validated on the first click for some reason , but besides that these values should not have to be added as they are null values in the database , yet the model still requires that they be added. Just wondering if anyone has any insight to this. Is there maybe a data annotation i can add like </p>
  
  <pre><code>[Required(this = false)]
  </code></pre>
  
  <p>To overwrite this.</p>
  
  <p>And also How can i get Brand and category to be validated on the first submit click?</p>
  
  <p>Thanks.</p>