Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:00:49+00:00 2026-06-06T17:00:49+00:00

I’m trying to create a drop down list that populates from a database. I

  • 0

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…!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:00:51+00:00Added an answer on June 6, 2026 at 5:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.