2017-10-04 49 views
0

控制器,用于局部视图DropDownListFor使用SelectListItems的ViewBag

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的 消息=错误执行处理程序进行处理 '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) 在ASP._Page_Views_Admin_Index_cshtml.Execute(c)中:\ Users \ lpjobray \ source \ repos \ Windows-MidTier \ WMTRedirectMgr \ WMTRedirectMgr \ Views \ Admin \ Index.cshtml:line 7 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage .E xecutePageHierarchy() 在System.Web.WebPages.StartPage.RunPage() 在System.Web.WebPages.StartPage.ExecutePageHierarchy() 在System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext中,TextWriter的作家,WebPageRenderingBase起始页) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult (ControllerContext上下文) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(ILi ST 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

您在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> 
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" }) 
+0

你不需要'htmlAttributes:' –

+1

[命名的参数(https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named - 和 - 可选参数)在这里不是必需的。'DropDownListFor'帮助程序正在使用预期的过载。在我的回答中解释了您遇到的问题。 – adiga

+0

我不知道,当我从原来的文章中改变它时,它开始工作... – JOb801