I having a problem understanding some of the datatypes and datasets with MVC and LINQ. I am trying to populate a dropdownlist.
I receive the following error (noted below in the code)
"Unable to create a constant value of type 'vps_intranet.Models.Part'. Only primitive types or enumeration types are supported in this context."
PartsController.cs
private List<Part> partsNotPartOfStructure(int partID)
{
Part mainPart = db.Parts.Find(partID);
//var alreadySelected = db.Parts.Select(p => p.PartStructures_comp).Distinct();
List<Part> parts = new List<Part>();
List<PartStructures> excludeList = db.PartStructures1
.Where(p => p.mainPart_id == partID).ToList();
parts = db.Parts.Where(c => c.PartStructures_comp
.Except((List<PartStructures>) excludeList)).ToList();
//The line above gives the error...
//Unable to create a constant value of type 'vps_intranet.Models.Part'.
//Only primitive types or enumeration types are supported in this context.**
return parts;
}
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Part part = await db.Parts.FindAsync(id);
if (part == null)
{
return HttpNotFound();
}
ViewData["AvailableParts"] = partsNotPartOfStructure(id.Value);
return View(part);
}
Details.cshtml
@model vps_intranet.Models.Part
@{
var fullList = (IEnumerable< vps_intranet.Models.Part >) ViewData["AvailableParts"];
var availableParts = fullList.Select(p => new SelectListItem { Text = p.partNo.ToString(), Value = p.id });
}
...
@Html.DropDownListFor(model => model.PartStructures_comp, availableParts));
What do I need to change?