{“FacilityID”:”18c9aebb-0874-4fc2-900e-48f6b192797f”,”FacilityRoomNumber”:102,”FacilityPurpose”:”DSFDSF”,”LabTypeID”:”bde03bf9-2fec-4d4c-bc00-e8fc605703a5″,”LabType”:{“LabTypeID”:”bde03bf9-2fec-4d4c-bc00-e8fc605703a5″,”LabTypeName”:”CT Scan”,”Facilities”:[]},”LabResults”:null}
LabType field is required.
I am creating a facility which has LabTypeID as the foreign key. I can read from the table which probably means all the FKs and tables are set correctly but I can add into database. Although LabType field has the values, the error says it is required,
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create([Bind(“FacilityRoomNumber,FacilityPurpose,LabTypeID”)] Facility facility)
{
facility.FacilityID = Guid.NewGuid();
var labtype = await _context.LabTypes.FindAsync(facility.LabTypeID);
if (labtype != null)
{
facility.LabType = labtype;
_context.Entry(facility).State = EntityState.Modified; // ensure ef tracks the changes
}
else
{
ModelState.AddModelError(string.Empty, "the specified lab type does not exist.");
return View(facility);
}
if (ModelState.IsValid)
{
Console.WriteLine("valid context");
_context.Facilities.Add(facility);
await _context.SaveChangesAsync();
return RedirectToAction("index", "facilities");
}
else
{
string json = JsonConvert.SerializeObject(facility, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
Console.WriteLine(json);
}
return View(facility);
}
huh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.