MVC中的动态DropDown列表 [英] Dynammic DropDown List in MVC

查看:100
本文介绍了MVC中的动态DropDown列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我需要你的帮助,我是MVC的新手,我正在尝试在MVC中创建一个CreateEmployee表单。现在,我想要实现的是将Departments的poulated下拉列表添加到表单中。 dropdownlist是从数据库填充的,使用visual studio,我连接到db,它创建了表的所有代码文件。这就是Createform应该是什么样子。下面的表单是使用Angular js创建的。



Dropbox中的图像



这是我的Createform模型。





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.


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();
    }





以下是visual studio生成的表格代码

< br $>
部门表





Here are the table codes that visual studio generated

Department Table

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





和部门表格背景





and the Department Table context

 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

推荐答案

将以下内容添加到模型中。



add the following to the model.

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





将以下内容添加到控制器



add the following to the controller

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

@Html.DropDownListFor(m => m.Department,
     Model.Departments.Select(d => new SelectListItem()
                                       {
                                           Value = d.id.ToString(),
                                           Text = d.Department 
                                       }))





回答链接


这篇关于MVC中的动态DropDown列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆