I have a database driven asp.net mvc application It has many foreign key relationships but for some reason for one of the tables I'm having issues with the foreign keys and the drop down boxes, when I want to create a new record I enter all the fields and I select a value in the drop down boxes which are foreign keys but when i click save it throws this error.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First
I know that it's the dropdownlist that causes this because I ran a try catch that made a .txt file which outputs the errors specific details as the stack trace hasn't got much detail. I've not done anything different to normal.
I've done this as per in the model
public virtual Employee Employee { get; set; }
public virtual Employee Employee1 { get; set; }
public virtual Fuel_Card Fuel_Card { get; set; }
public virtual Vehicle Vehicle { get; set; }
This as per in the controllers 1st create method
public ActionResult Create()
{
ViewBag.emp1 = new SelectList(db.Employees, "Employee_ID", "Employee_ID");
ViewBag.emp2 = new SelectList(db.Employees, "Employee_ID", "Employee_ID");
ViewBag.FuelCardNumber = new SelectList(db.Fuel_Card, "Fuel_Card_Number", "Fuel_Card_Number");
ViewBag.Registration = new SelectList(db.Vehicles, "Registration", "Registration");
return View();
}
This in the 2nd
ViewBag.emp1 = new SelectList(db.Employees, "Employee_ID", "Employee_ID", fuel_allocation.Requesting_Employee);
ViewBag.emp2 = new SelectList(db.Employees, "Employee_ID", "Employee_ID", fuel_allocation.Authorising_Employee);
ViewBag.FuelCardNumber = new SelectList(db.Fuel_Card, "Fuel_Card_Number", "Fuel_Card_Number", fuel_allocation.Fuel_Card_Number);
ViewBag.Registration = new SelectList(db.Vehicles, "Registration", "Registration", fuel_allocation.Registration_Number);
This inside the create view
<div class="editor-field">
@Html.DropDownList("emp2", String.Empty)
@Html.ValidationMessageFor(model => model.Authorising_Employee)
</div>
I was wondering if anyone has had this issue before? I've tried allsorts of different things, the foreign key relationship works when i use EditorFor but it always says it's empty when I try submit it when I use the dropdownlist.
I thought it might be using the same database field for 2 select lists but thats not the case as I've tested them individually
Here is the error I get when i output to file
02/01/2014 14:52:56: Entity of type "Fuel_Allocation" in state "Added" has the following validation errors: - Property: "Authorising_Employee", Error: "The Authorising_Employee field is required."