使用由外键链接的另一个模型的值填充DropDownListFor?

时间:2014-04-30 14:32:55

标签: c# sql-server asp.net-mvc linq entity-framework

我正在为我的公司构建一个Web应用程序。我的一个模型是员工模型employees1,它非常复杂。我遇到的一个问题是将值插入到phone_manager表中,该表绑定到phone_manager模型。 phone_manager模型与phone_types具有外键关系。 Phone_types包含系统中每种不同允许电话类型的条目。

在我的员工模型创建视图中,我创建了一个区域供用户输入他们的电话号码。电话号码有3个字段供用户填充,phone_typephone_numberphone_extension

我尝试做的是将phone_type的编辑器更改为下拉列表而不是带有整数值的文本框。下拉列表将从phone_types表中获取所有不同的电话类型。 phone_types表包含两个字段phone_type_idphone_type_name

我的简明模型代码如下:

[Table("employee.employees")]
public partial class employees1
{

    public employees1()
    {
        employee_phone_manager = new HashSet<phone_manager>();
    }

    [Key]
    public int employee_id { get; set; }
    [Display(Name = "Employee ID")]
    public int? assigned_id { get; set; }

    [Display(Name = "Phone Numbers")]
    public virtual ICollection<phone_manager> employee_phone_manager { get; set; }

    internal void CreatePhoneNumbers(int count = 1)
    {
        for (int i = 0; i < count; i++)
        {
            employee_phone_manager.Add(new phone_manager());
        }
    }

    public virtual ICollection<salary> salaries { get; set; }

    public class internalEmployees
    {
        public employees1 employees1 { get; set; }
        public phone_manager phone_manager { get; set; }
        public phone_types phone_types { get; set; }
    }

    [Table("employee.phone_manager")]
    public partial class phone_manager
    {
        [Key]
        public int phone_id { get; set; }

        public int employee_id { get; set; }

        [Required]
        [StringLength(15)]
        public string phone_number { get; set; }

        [StringLength(5)]
        public string phone_extension { get; set; }

        public int phone_type { get; set; }

        [Column(TypeName = "date")]
        public DateTime date_added { get; set; }

        public bool deleted { get; set; }

        public virtual employees1 employees1 { get; set; }
        [ForeignKey("phone_type")]
        public virtual phone_types phone_types { get; set; }
    }

    [Table("employee.phone_types")]
    public partial class phone_types
    {
        public phone_types()
        {
            phone_manager = new HashSet<phone_manager>();
        }

        [Key]
        public int phone_type_id { get; set; }

        [Required]
        [StringLength(50)]
        public string phone_type_name { get; set; }

        public virtual ICollection<phone_manager> phone_manager { get; set; }
    }

目前我的创建控制器是:

    // GET: /Employees/Create
    public ActionResult Create()
    {
        ViewBag.all_id = new SelectList(db.all_employees, "all_id", "all_id");
        var employee = new employees1();
        employee.CreatePhoneNumbers(2);
        return View(employee);
    }

我的观点的电话部分是:

    <div class="form-group" id="phoneNumbers">
        @Html.LabelFor(model => model.employee_phone_manager, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(x => x.employee_phone_manager)
        </div>
    </div>

使用以下模板:

@model AdminPanel.Models.employees1.internalEmployees
<div class="phoneNumber">
<p>
    <label>Phone Number</label>
    @Html.EditorFor(x => x.phone_manager.phone_type)
    @Html.EditorFor(x => x.phone_manager.phone_number)
    @Html.EditorFor(x => x.phone_manager.phone_extension)
    @Html.HiddenFor(x => x.phone_manager.deleted, new { @class = "mark-for-delete" })
</p>
</div>

如果我有x.phone_manager.phone_type的编辑器,我想要一个包含来自phone_types的所有值的组合框。

1 个答案:

答案 0 :(得分:0)

我发现了一个非常方便的教程,使用嵌套集合来实现我最初的目标 - &gt; http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html

从那时起,我设法将phone_types表格检索为SelectList并将其传递给我的ViewBag,如下所示:

ViewBag.phonetype = new SelectList(db.phone_types, "phone_type_id", "phone_type_name");

然后在我看来,我添加了以下内容:

@Html.DropDownListFor(x => x.phone_type, (SelectList)ViewBag.phonetype, new { @class = "form-control" })