Been at this far too long; trying to populate a field in a record with a foreign key, it is dependent on which user is logged on.
Shown below is a view bag which displayed the Firstname of the customer in a dropdown, which the user had to select. It would then put the CustomerID into the record when it was created. This method was impractical
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "FName");
Below is an attempt to input the foreign key automatically by finding the CustomerID and then inputting the custoemrID into a hidden field on a form.
public ActionResult Create()
{
var CusID = from c in db.Customers
where c.UserName == "User.Identity.Name"
select c.CustomerID;
return View(new CarAdvert { UserName = @User.Identity.Name, CustomerID = CusID });
}
The error: Cannot implicitly convert type system.linq.Iqueryable <-int-> to int
The approach taken (is this correct! – Any advice on the subject welcome)
any more info need please ask.
Regarding the error:
Instead of .Single you can/should use .First, .FirstOrDefault, … depending on your requirements.