gpt4 book ai didi

c# - MVC 下拉菜单需要 ienumerable?

转载 作者:行者123 更新时间:2023-11-30 15:32:36 25 4
gpt4 key购买 nike

从 MVC 中得到一些非常奇怪的行为,我想知道是我要发疯了还是我发现了一些奇怪的行为......

在我的 Razor View 中,我正在这样做......

@Html.DropDownListFor(p => p.SelectedMappingTypeId, Model.MappingTypes)

...我的 View 模型包含...

...
public SelectList MappingTypes { get; set; }

public int SelectedMappingTypeId { get; set; }
}

当我尝试运行这段代码时,我得到...

The ViewData item that has the key 'SelectedMappingTypeId' is of type 
'System.Int32' but must be of type 'IEnumerable'.

这肯定是不对的?

我认为您绑定(bind)到一个值类型和一个选择列表,生成的标记生成一个命名字段,该字段将为您提供从值类型属性中的选择列表中选择的项目值。

为什么它需要一个 ienumerable?

堆栈跟踪...

System.InvalidOperationException: The ViewData item that has the key 'SelectedMappingTypeId' is of type 'System.Int32' but must be of type 'IEnumerable'.• at System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name)
• at System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes)
• at System.Web.Mvc.Html.SelectExtensions.DropDownListFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes)
• at System.Web.Mvc.Html.SelectExtensions.DropDownListFor[TModel,TProperty](HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList)
• at ASP._Page_Views_ExtractMapping_MappingGrid_cshtml.Execute() in d:\Development\apps\Development_September\UI\nemo.RapidSystems\Views\ExtractMapping\MappingGrid.cshtml:line 24
• at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
• at System.Web.Mvc.WebViewPage.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.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
• at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
• at Emedia.Common.Mvc.Controllers.Helpers.NestedViewHelper.PartialFor[TModel,TProperty](HtmlHelper`1 helper, Expression`1 expression, String partialViewName) in D:\Development\apps\Development_September\Common\MVC\Controllers\Helpers\NestedViewHelper.cs:line 38
• at ASP._Page_Views_ExtractMapping_CampaignIteration_cshtml.Execute() in d:\Development\apps\Development_September\UI\nemo.RapidSystems\Views\ExtractMapping\CampaignIteration.cshtml:line 14
• 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.<>c__DisplayClass1c.b__19()
• at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
• at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.b__1b()
• at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
• at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
• at System.Web.Mvc.Controller.ExecuteCore()
• at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
• at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
• at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.b__5()
• at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.b__0()
• at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.b__7(IAsyncResult _)
• at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
• at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.b__d()
• at System.Web.Mvc.SecurityUtil.b__0(Action f)
• at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
• at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
• at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
• at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
• at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

最佳答案

如果 Model.MappingTypesnull,就会发生这种情况;它试图从第一个属性中找到 SelectList

关于c# - MVC 下拉菜单需要 ienumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18744486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com