【问题标题】:Populating a DropDownListFor with values from another model linked by a foreign key?用外键链接的另一个模型的值填充 DropDownListFor?
【发布时间】:2014-04-30 14:32:55
【问题描述】:

我正在为我的公司构建一个 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 个模型)。
  • @Jorge 我已经用我当前的所有相关代码完全重写了这个问题。希望现在更容易理解。
  • @Flater 我很抱歉,因为我从未使用过 MVC,但我正在努力掌握它。我的employees1 模型几乎包含单个模型中的所有内容,从我上面的代码中可以看出,但所有其他部分都是与之相关的部分。
  • 两种类型的局部视图有不同的方法。如果部分视图是“通用的”(例如“欢迎,用户 1”消息。)那是一个不相关的部分视图,应该有自己的模型。如果您有一个用于显示此特定视图内容的局部视图(例如,如果您有一个项目列表,则可以将特定详细信息呈现给另一个局部视图。因为该局部视图“属于”视图,所以它的模型应该可能是相同的,或者是原始视图的视图模型的孩子。但这是更好的做法,而不是使其工作的唯一方法。

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


【解决方案1】:

我找到了一个非常方便的教程,关于使用嵌套集合来实现我最初的目标 -> 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" })

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多