c# - 带有 Entity Framework 的 MVC 我的 DropDownList 不是下拉列表

标签 c# html asp.net-mvc entity-framework

我一直在关注 this向您展示如何填充下拉列表的教程。但是我的创建页面根本没有显示下拉列表,而是显示了这个

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>
}

用户 Controller .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);
}

最佳答案

你说你在跟风,但你并没有完全跟上。而是使用 @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 属性的名称(它不能与您绑定(bind)到的属性名称相同)

ViewBag.AdministratorList = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle")

关于c# - 带有 Entity Framework 的 MVC 我的 DropDownList 不是下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966436/

相关文章:

c# - 在C#中访问带有 "no name"(空格)的目录/文件夹

c# - 将 Winforms ListBox 集合绑定(bind)到 List<object>

c# - MVC6错误名称 'Configuration'在当前上下文中不存在

c# - 使用 System.Reflection 获取方法的全名

c# - 字符串格式化

asp.net - html 表格中不可选择的文本?

javascript - 我正在尝试向 HTML 表格中添加一些行,但失败了

javascript - Bootstrap 4 Button Group wrapping fix border-radius

c# - '' Unable to load DLL ' mfplat.dll' : The specified module could not be found.(HRESULT 异常:0x8007007E)”

asp.net-mvc - 如何将 Orchard CMS 与当前的 MVC2 应用程序集成?