用外键链接的另一个模型的值填充DropDownListFor
本文关键字:填充 DropDownListFor 模型 另一个 链接 | 更新日期: 2023-09-27 18:01:12
我正在为我的公司构建一个web应用程序。我的一个模型是员工模型employees1
,它非常复杂。我遇到的一个问题是将值插入到绑定到phone_manager
模型的phone_manager
表中。phone_manager
模型与phone_types
具有外键关系。Phone_types
包含系统中每种不同的允许电话类型的条目。
在我的员工模型创建视图中,我创建了一个区域,供用户输入他们的电话号码。电话号码有3个字段供用户填充,即phone_type
、phone_number
和phone_extension
。
我试图做的是将phone_type的编辑器更改为下拉列表,而不是具有整数值的文本框。下拉列表将从phone_types
表中获取所有不同的电话类型。phone_types
表包含两个字段phone_type_id
和phone_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
中所有值的组合框。
我发现了一个非常方便的教程,介绍如何使用嵌套集合来实现我最初想要的东西->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" })