I have a model with a foreignkey to another models. From this foreignkey, i want to show into my form a DropDownList with the name of my second models. When i go to create.cshtml or edit.cshtml i have the following error:
ArgumentNullException: Value cannot be null.
Here's my code:
// Models
public class Timesheet
{
public Pharmacy Pharmacy { get; set; }
}
// Controllers / ame code on edit.cshtml.cs
public IActionResult OnGet()
{
Dictionary<int, string> pharmacies = new Dictionary<int, string>();
foreach (Pharmacy p in _context.Pharmacy)
pharmacies.Add(p.PharmacyID, p.Name);
ViewData["PharmacyID"] = pharmacies;
return Page();
}
Into create/edit.cshtml, the HTML are:
@Html.DropDownListFor(model => model.ViewData["PharmacyID"], new SelectList(ViewBag.pharmacies , "key", "value"),"-- select --")
I hope you can help me to fix it :)
Thanks per advance !