MVC下拉需要不可数?

时间:2013-09-11 14:50:12

标签: c# asp.net-mvc razor

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

1 个答案:

答案 0 :(得分:4)

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