如何解决 尝试将元素作为与数组不兼容的类型进行访问
本文关键字:不兼容 数组 类型 访问 元素 何解决 解决 | 更新日期: 2023-09-27 17:57:24
我正在尝试在我的代码中使用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)
这是一个很长的机会,但我认为你应该尝试一下。
根据ArrayTypeMismatchException
的文档页面
当系统无法将元素转换为为数组声明的类型时,将引发 ArrayTypeMismatchException。例如,字符串类型的元素不能存储在 Int32 数组中,因为不支持这些类型之间的转换。
查看您提供的堆栈跟踪,我倾向于认为您传递给视图/网格的模型存在问题。从操作方法中可以看出,您正在传递AuthorizationWrapper
对象的列表,类型检查将阻止您在该列表中存储其他内容,因此问题介于两者之间。
首先在调试中检查视图的Model
属性;如果一切正常,只需下载源代码,在本地构建它并查看(在调试模式下)异常弹出的位置。这就是开源:)的重要部分
你需要把它包含在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>