Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC下拉列表需要ienumerable?_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# MVC下拉列表需要ienumerable?

C# MVC下拉列表需要ienumerable?,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,从MVC得到一些非常奇怪的行为,我想知道是我疯了还是我发现了一些奇怪的行为 在我看来,我正在这样做 @Html.DropDownListFor(p => p.SelectedMappingTypeId, Model.MappingTypes) 。。。我的视图模型包含 ... public SelectList MappingTypes { get; set; } public int SelectedMappingTypeId { get; set; } } 当我尝试

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

在我看来,我正在这样做

@Html.DropDownListFor(p => p.SelectedMappingTypeId, Model.MappingTypes)
。。。我的视图模型包含

...
    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'.
这肯定不对吗

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

为什么它需要一个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) 
System.InvalidOperationException:具有键“SelectedMappingTypeId”的ViewData项的类型为“System.Int32”,但必须为“IEnumerable”。•位于System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper HtmlHelper,字符串名称)
•位于System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper、字符串选项标签、字符串名称、IEnumerable`1选择列表、布尔allowMultiple、IDictionary`2 htmlAttributes)
•在System.Web.Mvc.Html.SelectExtensions.DropDownList中[TModel,TProperty](HtmlHelper`1 HtmlHelper,Expression`1 Expression,IEnumerable`1 selectList,String optionLabel,IDictionary`2 htmlAttributes)
•在System.Web.Mvc.Html.SelectExtensions.DropDownListFor[TModel,TProperty](HtmlHelper`1 HtmlHelper,Expression`1 Expression,IEnumerable`1 selectList)
•在d:\Development\apps\Development\u septer\UI\nemo.RapidSystems\Views\ExtractMapping\MappingGrid\cshtml第24行的ASP.\u Page\u Views\u ExtractMapping\MappingGrid\u cshtml.Execute()中
•位于System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
•位于System.Web.Mvc.WebViewPage.ExecutePageHierarchy()处
•位于System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext、TextWriter writer、WebPageRenderingBase起始页)
•位于System.Web.Mvc.RazorView.RenderView(ViewContext、TextWriter、Object实例)
•位于System.Web.Mvc.buildmanager compiledview.Render(ViewContext、ViewContext、TextWriter)
•位于System.Web.Mvc.HtmlHelper.RenderPartialInternal(字符串partialViewName、ViewDataDictionary viewData、对象模型、TextWriter编写器、ViewEngineCollection ViewEngineCollection)
•位于System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper HtmlHelper、字符串partialViewName、对象模型、ViewDataDictionary viewData)
•在Emedia.Common.Mvc.Controllers.Helpers.NestedViewHelper.partialford:\Development\apps\Development\u septer\Common\Mvc\Controllers\Helpers\NestedViewHelper.cs第38行中的[TModel,TProperty](HtmlHelper`1 helper,Expression`1 Expression,String partialViewName)中
•在d:\Development\apps\Development\u septer\UI\nemo.RapidSystems\Views\ExtractMapping\activationIteration\u cshtml.Execute()中的ASP.\u页面\u视图\u提取映射\u活动迭代\u cshtml.Execute():第14行
•位于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起始页)
•位于System.Web.Mvc.RazorView.RenderView(ViewContext、TextWriter、Object实例)
•位于System.Web.Mvc.buildmanager compiledview.Render(ViewContext、ViewContext、TextWriter)
•位于System.Web.Mvc.ViewResultBase.ExecuteSult(ControllerContext上下文)
•位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)
•在System.Web.Mvc.ControllerActionInvoker.c__DisplayClass1c.b__19()中
•位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
•在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass1C.c_uuuDisplayClass1E.b_uu1B()中
•位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1过滤器,ActionResult ActionResult)
•位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)
•在System.Web.Mvc.Controller.ExecuteCore()上
•在System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)上
•位于System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(请求上下文请求上下文)
•在System.Web.Mvc.MvcHandler.c_uudisplayClass6.c_udisplayClassB.b_u5()中
•在System.Web.Mvc.Async.AsyncResultRapper.c__DisplayClass1.b__0()中
•在System.Web.Mvc.Async.AsyncResultRapper.c_uuu显示类8`1.b_uuu7(IAsyncResult)
•位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`1.End()
•在System.Web.Mvc.MvcHandler.c_udisplayClasse.b_ud()中
•在System.Web.Mvc.SecurityUtil.b___0上(操作f)
•位于System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作)
•位于System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
•在System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest中(IAsyncResult结果)
•在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()中
•在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)

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

堆栈跟踪是什么?不确定是否有帮助。。。英雄联盟