I have the following ViewModel (just showing few properties for easy reading)
public class Contract{
public string Name {get; set;}
public ContractTerm PrpTerm { get; set; }
}
the PRP Term model is something like this
public class PrpTerm{
public int Id {get; set;}
public string Name {get; set;}
}
For my model validations I'm using an XML file for injecting the annotations. I've set the Name property as required and I've let ContractTerm without specifying any validation (it's an optional field on a list). My view is something has a textbox for the Name and a select list for the Contract term.
When I'm trying to save, I get the ModelState.IsValid property as false, saying that the PRPTerm.Id value is required. I understand that it is since we cannot assing a null value to a int.
Does somebody know a way to avoid this to be validated?
I've tried using the Bind(Exclude annotation, but this removes the field from the data submission and this doesn't allow me to get the value the user selected (if that happened). I cannot set the id as nullable on the PrpClass. The other option is set the PrpTerm Id on the ViewModel but I need to justify that change with a valid reason.
Any idea?