首页 > model-view-controller > 具有键'Key'的ViewData项的类型为'System.Int32',但在iis7.5中必须是'IEnumerable <Selec

具有键'Key'的ViewData项的类型为'System.Int32',但在iis7.5中必须是'IEnumerable <Selec (The ViewData item that has the key 'Key' is of type 'System.Int32' but must be of ty)

问题

解决 - 检查评论

我将代码部署到IIS 7.5时出现问题,代码在localhost中工作正常但在部署时崩溃。

我有两个几乎完全相同的方法,其中一个在localhost和IIS中都可以工作,一个只能在localhost中工作

适用于IIS 7.5和localhost

调节器

var CustomerList = (from Cust in db.Customers
                   select Cust).ToList();
ViewBag.CustomerList = new SelectList(CustomerList, "CustomerId", "Name");

视图

@Html.DropDownListFor(m => m.CustomerId, ViewBag.CustomerList as SelectList, "-Select Customer-", new { id = "CustomerList" })

模型

    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        public Customer()
        {
            this.Applications = new HashSet<Application>();
            this.ChangeRequests = new HashSet<ChangeRequest>();
            this.Contacts = new HashSet<Contact>();
        }

        public int CustomerId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
        public Nullable<bool> Active { get; set; }

        public virtual ICollection<Application> Applications { get; set; }
        public virtual ICollection<ChangeRequest> ChangeRequests { get; set; }
        public virtual ICollection<Contact> Contacts { get; set; }
    }

不在IIS 7.5中工作,但在localhost中工作

调节器

var ModificationList = (from m in db.Modifications
                        select m).ToList();
ViewBag.ModificationList = new SelectList(ModificationList, "ModificationId", "Code");

视图

@Html.DropDownListFor(m => m.ModificationId, ViewBag.ModificationList as SelectList, "-Select Change-", new { id = "ModificationList" })

模型

    using System;
    using System.Collections.Generic;

    public partial class Modification
    {
        public Modification()
        {
            this.ChangeRequests = new HashSet<ChangeRequest>();
        }

        public int ModificationId { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }

        public virtual ICollection<ChangeRequest> ChangeRequests { get; set; }
    }

尝试进入已部署的站点时出现的错误

> The ViewData item that has the key 'ModificationId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. 

> [InvalidOperationException: The ViewData item that has the key 'ActionId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.]
   System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name) +442
   System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +186
   System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +136
   ASP._Page_Views_ChangeRequest_CreateChangeView_cshtml.Execute() in d:\WebSites\TeamWeb\ChangeRequestSite\Test\Views\ChangeRequest\CreateChangeView.cshtml:153
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +181
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

解决方法

问题是.dll文件在iis部署中没有正确更新,因此控制器更改从未更新,但视图更改了。所以错误只是声明ViewBag为null。我添加了“if(ViewBag.ModificationList!= null){'Dropdown'}语句,当下拉列表没有显示时我发现了问题。希望这有助于将来的其他人!

问题

Solved - Check comments

I have a problem when I deploy my code to IIS 7.5, The code works fine in localhost but crashes when I deploy it.

I have two methods that are almost exactly the same where one of them works both in localhost and IIS and one only works in localhost

Works in IIS 7.5 and localhost

Controller

var CustomerList = (from Cust in db.Customers
                   select Cust).ToList();
ViewBag.CustomerList = new SelectList(CustomerList, "CustomerId", "Name");

View

@Html.DropDownListFor(m => m.CustomerId, ViewBag.CustomerList as SelectList, "-Select Customer-", new { id = "CustomerList" })

Model

    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        public Customer()
        {
            this.Applications = new HashSet<Application>();
            this.ChangeRequests = new HashSet<ChangeRequest>();
            this.Contacts = new HashSet<Contact>();
        }

        public int CustomerId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
        public Nullable<bool> Active { get; set; }

        public virtual ICollection<Application> Applications { get; set; }
        public virtual ICollection<ChangeRequest> ChangeRequests { get; set; }
        public virtual ICollection<Contact> Contacts { get; set; }
    }

.

.

Don't Work in IIS 7.5 but works in localhost

Controller

var ModificationList = (from m in db.Modifications
                        select m).ToList();
ViewBag.ModificationList = new SelectList(ModificationList, "ModificationId", "Code");

View

@Html.DropDownListFor(m => m.ModificationId, ViewBag.ModificationList as SelectList, "-Select Change-", new { id = "ModificationList" })

Model

    using System;
    using System.Collections.Generic;

    public partial class Modification
    {
        public Modification()
        {
            this.ChangeRequests = new HashSet<ChangeRequest>();
        }

        public int ModificationId { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }

        public virtual ICollection<ChangeRequest> ChangeRequests { get; set; }
    }

The Error I get when trying to enter the deployed site

> The ViewData item that has the key 'ModificationId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. 

.

> [InvalidOperationException: The ViewData item that has the key 'ActionId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.]
   System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name) +442
   System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +186
   System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +136
   ASP._Page_Views_ChangeRequest_CreateChangeView_cshtml.Execute() in d:\WebSites\TeamWeb\ChangeRequestSite\Test\Views\ChangeRequest\CreateChangeView.cshtml:153
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +181
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

解决方法

The problem was that that the .dll files didnt update correctly in the iis deploy, so the controller changes never was updated but the view changes were. So the error just stated that the ViewBag was null. I added "if(ViewBag.ModificationList != null){'Dropdown'} statement and when the dropdown did not show I figured out the problem. Hope this helps someone else in the future!

相似信息