[asp.net-mvc]MVC 与实体框架我下拉列表不是下拉列表

发布时间: 2017/3/25 3:34:58
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我一直在关注这个教程,显示你如何填充下拉列表。然而我创建页面根本不显示一个下拉列表中的,但表明这

enter image description here

这是我下拉列表代码在我的 UserController.cs

private void PopulateAdministratorsDropDownList(object selectedAdministrator = null)
{
    var administratorQuery = from d in db.Administrators
                           orderby d.AdministratorTitle
                             select d;
    ViewBag.AdministratorID = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle", selectedAdministrator);
}

Views\User\Create.cshtml

@model RecreationalServicesTicketingSystem.Models.User
....
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        .... // more controls
        <div class="editor-label">
            @Html.LabelFor(model => model.AdministratorID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AdministratorID)
            @Html.ValidationMessageFor(model => model.AdministratorID)
        </div>
        ....
        <p>
            <input type="submit" value="Create" />
        </p>

    </fieldset>
}

UserController.cs

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

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "LastName, FirstMidName, EnrollmentDate,AdministratorID, DepartmentID, DepotID")]User user)
{
    try
    {
        if (ModelState.IsValid)
        {
            db.Users.Add(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
    catch (DataException /* dex */)
    {
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }
    PopulateAdministratorsDropDownList(user.AdministratorID);
    return View(user);
}

private void PopulateAdministratorsDropDownList(object selectedAdministrator = null)
{
    var administratorQuery = from d in db.Administrators
                             orderby d.AdministratorTitle
                             select d;
    ViewBag.AdministratorID = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle", selectedAdministrator);
}

解决方法 1:

你说你在追随啧啧,但不严格地遵守它。改为使用 @Html.DropDownListFor 当你想要得到一个下拉列表。

在链接您张贴他们使用以下 Department 下拉; 像这样︰

<div class="editor-label">
    @Html.LabelFor(model => model.DepartmentID, "Department")
</div>
<div class="editor-field">
    @Html.DropDownList("DepartmentID", String.Empty)
    @Html.ValidationMessageFor(model => model.DepartmentID)
</div>

但你看起来像︰

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

他们不是等效的。

边注︰ 你之后不是本教程按照最佳实践 (使用强类型 DropDownListFor() 方法),和使用 DropDownList() 也将意味着你做不到任何客户端端验证 (如果你检查生成的 html,你会注意到有没有 data-val-* 属性在 <select> 标记)。相反,使用

@Html.DropDownListFor(m => m.AdministratorID, (SelectList)ViewBag.AdministratorList, "Please select")

和改变 (它不能作为属性相同的名称您绑定到) 你 ViewBag 属性的名称

ViewBag.AdministratorList = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle")
官方微信
官方QQ群
31647020