Search code examples
asp.netasp.net-mvc-4ef-code-first

Allow null in model when using Code first


I have an model like this:

 public class EquipmentEmployee
{
    public int EquipmentEmployeeID { get; set; }

    public int EmployeeID { get; set; }
    public Employee Employee { get; set; }

    public int EquipmentID { get; set; }
    public Equipment Equipment { get; set; }

    public int RequisitionID { get; set; }
    public Requisition Requisition { get; set; }

    public DateTime From { get; set; }

    public DateTime To { get; set; }

    public string Comment { get; set; }

}

I use Mvc scaffolding for creating my controllers, repositories and views. Int the create View I'm not able to POST since I dont have values for "to" and "RequisitionID". I have not added [Required] to them. Is this possible? To POST and have those two null?


Solution

  • You should declare optional fields using a nullable type

    public int? RequisitionID { get; set; }