在 asp.net mvc 4 视图中插入下拉列表,数据将来自另一个 table

insert dropdown in asp.net mvc 4 view, and the data will be from another table

我是 asp.net 的初学者,所以我可能有愚蠢的问题,但我需要一些帮助。 我有一个模型和控制器 MAIN,具有 read/write 规则。 我在 MainController 中有一个创建操作,它可以工作,但我需要其中一个字段是下拉列表,并且它有来自部门的另一个 table 的选项。 我已经搜索了答案,但没有得到完整的答案。 所以如果可以的话,请帮助我。 如果需要我会写代码。 非常感谢。

主控制器

 public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Main/Create

        [HttpPost]
        public ActionResult Create(Main main)
        {
            if (ModelState.IsValid)
            {
                db.Main.Add(main);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(main);
        }

Main.cs(型号)

    namespace PhoneBook.Models
{
    public class Main
    {
        public int Id { get; set; }
        public string F_L_Name { get; set; }
        public string Regioni { get; set; }
        public string Raioni { get; set; }
        public string Department { get; set; }
        public string Division { get; set; }
        public string Position { get; set; }
        public string Mobile { get; set; }
        public string I_Phone { get; set; }
        public string Email { get; set; }
        public int Deleted { get; set; }
        public string Comment { get; set; }
        public string All_In_One { get; set; }
        public DbSet<Department> DepartmentFI { get; set; }
    }
}

Create.cshtml(查看)

    @model PhoneBook.Models.Main

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Main</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.F_L_Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.F_L_Name)
            @Html.ValidationMessageFor(model => model.F_L_Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Regioni)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Regioni)
            @Html.ValidationMessageFor(model => model.Regioni)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Raioni)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Raioni)
            @Html.ValidationMessageFor(model => model.Raioni)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Department)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Department)
            @Html.ValidationMessageFor(model => model.Department)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Division)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Division)
            @Html.ValidationMessageFor(model => model.Division)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Position)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Position)
            @Html.ValidationMessageFor(model => model.Position)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Mobile)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Mobile)
            @Html.ValidationMessageFor(model => model.Mobile)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.I_Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.I_Phone)
            @Html.ValidationMessageFor(model => model.I_Phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

像这样使用将部门显示为下拉列表

@Html.DropDownListFor(模型=>model.Department,模型=>model.Departments)

注意:您需要使用 controller/service

中数据库 table 的值填充 model.Departments