Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
please i need your help, i am very new to MVC and i am trying to make a CreateEmployee form in MVC. for now, all i am trying to achieve is to add the poulated dropdownlist for Departments to the form. the dropdownlist is populated from a database , and using visual studio, i connected to the db and it created all the code file for the table . This is what the Createform should look like. The form below is created using Angular js.

image in dropbox

here is my Createform model.


C#
public class CreateEmployee
   {
       public int Id { get; set; }
       public string FullName { get; set; }
       [Required]
       public string Notes { get; set; }

      //add the dropdown list of departments here,i not sure on what to do here
      //do i create an instance of dbcontext here or AngtestDepartment


       public bool PerkCar { get; set; }
       public bool PerkStock { get; set; }
       public bool PerkSixWeeks { get; set; }
       public string PayrollType { get; set; }
   }

    public ActionResult CreateEmployee()
    {


      return View();
    }


Here are the table codes that visual studio generated

Department Table

C#
public partial class AngTestDepartment
{
    public int id { get; set; }
    public string Department { get; set; }
}


and the Department Table context

C#
 using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

public partial class DepartmentDbContext : DbContext
{
    public DepartmentDbContext()
        : base("name=DepartmentDbContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<AngTestDepartment> AngTestDepartments { get; set; }

    public System.Data.Entity.DbSet<AngularForms2.Models.CreateEmployee> CreateEmployees { get; set; }
}



Thank you very much for your help, i appreciate it
Posted

1 solution

add the following to the model.

C#
public int Department { get; set; }
public IEnumerable<angtestdepartment> Departments { get; set; }</angtestdepartment>


add the following to the controller
C#
public ActionResult CreateEmployee() 
{
    using (var db = new DepartmentDbContext())
    {
        var model = new CreateEmployee();
        model.Departments = db.AngTestDepartments.ToList();
        return View(model);
    }
}


add the following to the view
C#
@Html.DropDownListFor(m => m.Department,
     Model.Departments.Select(d => new SelectListItem()
                                       {
                                           Value = d.id.ToString(),
                                           Text = d.Department 
                                       }))


answer link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900