Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor, MVC4, @html.dropdownlistfor problems

I'm trying to create a drop down list that populates from a database. I have:

public class Employee
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public String FirstName { get; set; }
        [Required]
        public String LastName { get; set; }
        [Required]
        public String JobTitle { get; set; }
    }

    public class Project
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public String ProjectName { get; set; }
        [Required]
        public String CompanyName { get; set; }
    }

    public class ProjectHour
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public decimal Hours { get; set; }
        [Required]
        public DateTime Date { get; set; }
        public virtual ICollection<Employee> employeeId { get; set; }
        public virtual ICollection<Project> projectId { get; set; }
    }

What I want is to create a form that will create new project hours associated with a project and an employee. I'm trying to use dropdownlists to display the employees and the projects on the create form. Obviously, I'm completely new at this, but what I have so far is:

[HttpPost]
     public ActionResult CreateProjectHour(ProjectHour newProjectHour)
     {
             using (var db = new TimesheetContext())
             {
                 IEnumerable<SelectListItem> emp = db.Employees
                  .Select(c => new SelectListItem
                  {
                      Value = c.Id.ToString(),
                      Text = c.LastName
                  });
                 ViewBag.EmployeeId = emp;

                 db.ProjectHours.Add(newProjectHour);
                 db.SaveChanges();
             }

             return RedirectToAction("ProjectHourList");
         }
     }

And on the form:

@model TimesheetMVC.Models.ProjectHour
...
@Html.DropDownListFor(model => model.employeeId, (SelectList)ViewBag.EmployeeId)

Which is apparently horribly wrong. Any help would be much appreciated...!

like image 801
Andrew Clear Avatar asked Jun 27 '12 16:06

Andrew Clear


2 Answers

Don't use the same name EmployeeId. You need 2 things to create a dropdown list in ASP.NET MVC: a scalar property that will hold the selected value and a collection property that will contain the possible values. But since you are using the ViewBag (which I totally recommend against) you could do the following:

ViewBag.Employees = emp;

and in your view:

@Html.DropDownListFor(
    model => model.employeeId, 
    (IEnumerable<SelectListItem>)ViewBag.Employees
)

But as I said this is not at all an approach that I recommend. I recommend using view models. So define an IEnumerable<SelectListItem> property on your view model:

public IEnumerable<SelectListItem> Employees { get; set; }

and in your controller populate this view model property and then make your view strongly typed to the view model.

like image 96
Darin Dimitrov Avatar answered Sep 22 '22 18:09

Darin Dimitrov


Or you could just do a in the controller

SelectList selectList = new SelectList(db.Employees, "Id", "LastName");
ViewBag.EmployeeList = selectList;

and in the View

@Html.DropDownBoxFor(model => model.id_Employee, ViewBag.EmployeeList as SelectList)

I find this approach easier.

like image 43
Luis Robles Avatar answered Sep 26 '22 18:09

Luis Robles