0
votes

Controller for partial view

    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();
}

Snippet of partial view for drop down list

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

Calling the partial view from Index.cshtml

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

No compile errors with this, but when the page loads, I get this exception:

System.Web.HttpException occurred HResult=0x80004005 Message=Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. Source=System.Web.Mvc StackTrace: at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, Object routeValues) at ASP._Page_Views_Admin_Index_cshtml.Execute() in 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.ExecutePageHierarchy() at System.Web.WebPages.StartPage.RunPage() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 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 context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, 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.

If I change Html.DropDownListFor to add the list items statically with new SelectListItem, everything renders fine.

2

2 Answers

2
votes

You have a redundant ) after IEnumerable<SelectListItem>. It's closing the DropDownListFor without the object intended for htmlAttributes parameter. Change it to:

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

I had a syntax error defining the htmlAttributes bit. Changing to this resolved the problem.

        @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" })