3

从 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'.

这肯定不对吧?

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

为什么它需要一个可枚举的?

堆栈跟踪 ...

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

1 回答 1

4

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

于 2013-09-11T15:14:54.817 回答