【问题标题】:DropDownListFor using ViewBag of SelectListItemsDropDownListFor 使用 SelectListItems 的 ViewBag
【发布时间】:2017-10-04 17:23:33
【问题描述】:

局部视图控制器

    public ActionResult _AddRedirect()
{
    var domains = (from d in DB.Tokens
                    orderby d.Domain
                    select d).Distinct().Select(d => new SelectListItem
                    {
                        Value = d.Domain,
                        Text = d.Domain

                    });
    ViewBag.DomainList = domains.AsEnumerable();

    return PartialView();
}

下拉列表的部分视图片段

        <div class="col-md-10">
    @Html.DropDownListFor(
         model => model.Domain,
         ViewBag.DomainList as IEnumerable<SelectListItem>),
         new {@class="form-control dropdown"}
     )
    @Html.ValidationMessageFor(model => model.Domain, "", new { @class = "text-danger" })
    </div>

从 Index.cshtml 调用局部视图

    @Html.Action("_AddRedirect", new ViewDataDictionary())

没有编译错误,但是当页面加载时,我得到了这个异常:

发生 System.Web.HttpException H结果=0x80004005 消息=执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。 源=System.Web.Mvc 堆栈跟踪: 在 System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper,字符串 actionName,字符串 controllerName,RouteValueDictionary routeValues,TextWriter textWriter) 在 System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,字符串 actionName,字符串 controllerName,RouteValueDictionary routeValues) 在 System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,字符串 actionName,对象 routeValues) 在 C:\Users\lpjobray\source\repos\Windows-MidTier\WMTRedirectMgr\WMTRedirectMgr\Views\Admin\Index.cshtml 中的 ASP._Page_Views_Admin_Index_cshtml.Execute():第 7 行 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 在 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 在 System.Web.WebPages.StartPage.RunPage() 在 System.Web.WebPages.StartPage.ExecutePageHierarchy() 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage) 在 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object 实例) 在 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer) 在 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext 上下文) 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult) 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 过滤器, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

Inner Exception 1:
HttpParseException: "class" is a reserved word and cannot be used in implicit expressions.  An explicit expression ("@()") must be used.

如果我更改 Html.DropDownListFor 以使用新的 SelectListItem 静态添加列表项,则一切正常。

【问题讨论】:

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


    【解决方案1】:

    IEnumerable&lt;SelectListItem&gt; 后面有一个多余的)。它正在关闭 DropDownListFor 而没有用于 htmlAttributes 参数的对象。改成:

    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Domain,
            ViewBag.DomainList as IEnumerable<SelectListItem>,
            new {@class="form-control dropdown"}
        )
        @Html.ValidationMessageFor(model => model.Domain, "", new { @class = "text-danger" })
    </div>
    

    【讨论】:

      【解决方案2】:

      我在定义 htmlAttributes 位时遇到了语法错误。改成这个解决了问题。

              @Html.DropDownListFor(
              model => model.Domain,
              ViewBag.DomainList as IEnumerable<SelectListItem>,
              htmlAttributes: new { @class = "form-control dropdown" })
          @Html.ValidationMessageFor(model => model.Domain, "", new { @class = "text-danger" })
      

      【讨论】:

      • 你不需要htmlAttributes:
      • Named arguments 在这里不是必需的。 DropDownListFor 助手正在使用预期的重载。我的回答中解释了您面临的问题。
      • 我不知道,当我从原来的帖子中更改它时,它开始工作了......
      • @JOb801 您的原始帖子有一个额外的右括号,导致错误。 ViewBag.DomainList as IEnumerable&lt;SelectListItem&gt;), htmlAttributes: 部分,您的 DropDownListFor 将正常工作。错误是由于new {@class="form-control dropdown"} 不在DropDownListFor() 范围内
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多