【问题标题】:how to solve Attempted to access an element as a type incompatible with the array如何解决尝试以与数组不兼容的类型访问元素
【发布时间】:2019-02-07 23:01:00
【问题描述】:

我正在尝试在我的代码中使用Grid.MVC。这个例子看起来很简单,但是当我尝试使用它时,我不断收到ArrayTypeMismatchException

这是我的代码。关于如何解决它的任何想法?

//This is the my cshtml code.

 @Html.Grid(Model).Columns(columns => 
    {
        columns.Add(c => c.Name).Titled("Name");
        columns.Add(c => c.Operation).Titled("Operation").Filterable(true);
    }).WithPaging(3).Sortable(true)



//This is my controller code.

public ActionResult ListAllAuthorization()
{
    IList<AuthorizationWrapper> authorizations;
    using (GenericRepositoryV2 repo = new GenericRepositoryV2())
    {
        authorizations = repo.GetAllAuthorization();

    }
    return View(authorizations);
}

如果有什么不同,我的配置:

  • MVC 5
  • .Net 框架 4.5

更新 1: 堆栈跟踪:

at System.Collections.Generic.List`1.set_Item(Int32 index, T value)
at System.Collections.ObjectModel.Collection`1.SetItem(Int32 index, T item)
at System.Web.Routing.RouteCollection.SetItem(Int32 index, RouteBase item)
at System.Collections.ObjectModel.Collection`1.set_Item(Int32 index, T value)
at System.Web.Mvc.ControllerContext.get_RequestContext()
at GridMvc.Html.GridHtmlOptions`1.RenderPartialViewToString(String viewName, Object model, ViewContext viewContext)
at GridMvc.Html.GridHtmlOptions`1.ToHtmlString()
at System.Web.HttpUtility.HtmlEncode(Object value)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, Object content)
at System.Web.WebPages.WebPageBase.Write(Object value)
at ASP._Page_Views_Admin_ListAllAuthorization_cshtml.Execute() in e:\Oman-Erp\OmanERP\Views\Admin\ListAllAuthorization.cshtml:line 28
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.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

【问题讨论】:

  • 请显示完整的堆栈跟踪。我们不知道哪个部分实际上是失败的。
  • @Manpreet Singh,repo.GetAllAuthorization() 有什么作用?你能发布异常的代码和完整的堆栈跟踪吗?
  • @RePierre GetAllAuthorization() 返回一个 IList&lt;AuthorizationWrapper&gt;
  • @JonSkeet 添加了堆栈跟踪。自己无法从中收集到任何可操作的信息。
  • 除此之外,它似乎在ControllerContext.RequestContext getter 中,我不希望它与您显示的代码有任何关系。绝对值得拥有 - 虽然我自己知道的还不够多。

标签: c# asp.net-mvc-5


【解决方案1】:

这是一个很长的尝试,但我认为你应该尝试一下。

根据documentation page of ArrayTypeMismatchException

当系统无法将元素转换为为数组声明的类型时,将引发 ArrayTypeMismatchException。例如,String 类型的元素不能存储在 Int32 数组中,因为不支持这些类型之间的转换。

查看您提供的堆栈跟踪,我倾向于认为您传递给视图/网格的模型存在问题。从 action 方法中可以看出,您传递了一个 AuthorizationWrapper 对象列表,并且类型检查会阻止您在该列表中存储其他内容,因此问题介于两者之间。

首先在调试中检查视图的Model 属性;如果一切正常,只需下载source code,在本地构建它并查看(在调试模式下)异常弹出的位置。这就是开源的伟大之处:)

【讨论】:

  • 就我而言,我已经有效地完成了BaseType[] items = new[] { new ActualType() };,所以虽然我认为我正在处理BaseType 的数组,但实际上实现的是ActualType。解决方案当然是在创建时显式键入数组。
【解决方案2】:

您需要将其包含在 web.config 中

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2021-11-18
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多