I have one to many relationship in the application. I want to display data in the view using ViewBag, but instead of data from the database I get the path name of the model class. My code:
public class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public ICollection<Instructor> Instructor { get; set; }
}
[Key]
public int InstructorId { get; set; }
public string NameInstructor { get; set; }
public string Email { get; set; }
public string ScienceDegree { get; set; }
public Department Department { get; set; }
Controller class:
[HttpGet]
public ActionResult Create()
{
var item = db.Departments.ToList();
ViewBag.DepartmentName = new SelectList(item, "DepartmentName");
return View();
}
View class:
@Html.DropDownList("DepartmentName", new SelectList(ViewBag.DepartmentName, "DepartmentName"))
As a result, I get it instead of names. I also did according to guides, step by step but it does not help, unfortunately I have no idea what to do.