DropDownListFor使用SelectListItems

时间:2017-10-04 17:23:34

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

部分视图控制器

    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         的HResult = 0x80004005的         Message =执行处理程序'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错。         来源= System.Web.Mvc         堆栈跟踪:          在System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper,String actionName,String controllerName,RouteValueDictionary routeValues,TextWriter textWriter)          在System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,String actionName,String controllerName,RouteValueDictionary routeValues)          在System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,String actionName,Object routeValues)          在ASP:_Page_Views_Admin_Index_cshtml.Execute()在C:\ Users \ lpjobray \ source \ repos \ Windows-MidTier \ WMTRedirectMgr \ WMTRedirectMgr \ Views \ Admin \ Index.cshtml:第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 instance)          在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)          在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1个过滤器,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静态添加列表项,则所有内容都可以正常显示。

2 个答案:

答案 0 :(得分:2)

)之后您有一个多余的IEnumerable<SelectListItem>。如果没有用于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>

答案 1 :(得分:0)

我在定义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" })