【发布时间】: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(IList
1 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