ModelState is marked invalid for an empty non-required field
asp.net-mvc, asp.net-mvc-3, asp.net-mvc-4
Solution
My first guess would be that it is throwing an exception that you're not setting year and it's null? if you made year a Nullable<int> does it not throw the required message?
I still wouldn't expect it to be required, it's a shot in the dark
Problem
A bit of a mystery. I have a viewmodel with a Year property: ``` public class TradeSpendingSalesViewModel { public string ProductCode { get; set; } public IEnumerable<SelectListItem> AllowTypeSelect { get; set; } public string AllowType { get; set; } public IEnumerable<SelectListItem> YearsSelect { get; set; } public int Year { get; set; } } ``` If I post an empty viewmodel to my controller: ``` [HttpPost] public ActionResult Index(TradeSpendingSalesViewModel vm) { var allErrors = ModelState.Values.SelectMany(v => v.Errors); foreach (var e in allErrors) { Response.Write(e.ErrorMessage); } } ``` Then I get a single error with a message of: "The Year field is required." Since I haven't annotated the viewmodel Year field with the `Required` attribute, I'm unclear why this error is being generated. Any ideas?