【问题标题】:Create an HTML Dropdown in Layout in ASP.NET MVC 5在 ASP.NET MVC 5 的布局中创建 HTML 下拉列表
【发布时间】:2021-12-14 23:20:53
【问题描述】:

我有一个仪表板,sidenav 在 layout.cshtml 中。我有一个模式,其中包含一个在主页/索引中工作的下拉列表。但是,在不同的页面上,模式不会触发。然后我发现,模态框需要在布局中,以便可以从任何页面触发,因为 sidenav 中的链接。

但是,当我将模态框移到 layout.cshtml 时,出现错误

Line 186: @Html.DropDownList("DomainDropDownList", ViewBag.domainSelectionList as SelectList, new { @class = "select" })

[InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DomainDropDownList'.]

然后我意识到这是因为没有 ViewData,因为它在 layout.cshtml 中。

我查了一下,发现我可以使用 ViewComponents,但那是在 Core 中,我没有使用 Core。

是否有人对我如何在 layout.cshtml 中使用以下方法获取下拉列表有任何建议:

SelectList DomainDropDownList = new SelectList(DomainManagement.ListIsDomainAlive(), "id", "domainName");
ViewBag.domainSelectionList = DomainDropDownList;

谢谢

【问题讨论】:

    标签: c# .net asp.net-mvc


    【解决方案1】:

    你应该改变

    @Html.DropDownList("DomainDropDownList", ViewBag.domainSelectionList as SelectList, new { @class = "select" })

    @Html.DropDownList("domainSelectionList", ViewBag.domainSelectionList as SelectList, new { @class = "select" })

    因为ViewBag 名称是domainSelectionList 而不是DomainDropDownList

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多