2

我目前在我的视图文件的 ViewBag 中遇到一个非常奇怪的错误。

System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

第 6 行是错误的原因:

Line 4:      ViewBag.Title = "Home";
Line 5:      ViewBag.StyleSheet = "Main/Home";
Line 6:      ViewBag.Description = "This description will be used by the _ViewStart page.";

我试过删除第 6 行,但它会在第 5 行抛出错误。

我完全不知道 ViewBag 是如何导致此类错误的,尤其是字符串。有没有办法解决这个问题?

编辑:在视图文件中没有什么可看的。但这里还有更多:

@using Company.Product.Extenstions; // Just noticed the misspelling there...

@{
    ViewBag.Title = "Home";
    ViewBag.StyleSheet = "Main/Home";
    ViewBag.Description = "This description will be used by the _ViewStart page.";
}

<div class="hero">
    <div class="content-wrapper">
        <div id="most-downloaded">
            <h2>Most Downloaded</h2>

和控制器功能:

public ActionResult Index()
{
    ViewBag.MostDownloaded = ItemService.GetItems(ItemType.Document, "downloads", "desc", 6);
    ViewBag.MostRecent = ItemService.GetItems(ItemType.Document, "time", "desc", 4);

    ViewBag.OnlineUsers = HttpContext.Application["OnlineUsers"];
    ViewBag.TotalUsers = UserService.GetUsersCount();
    ViewBag.TotalItems = ItemService.GetItemCount();

    return View();
}

堆栈跟踪:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.ThrowHelper.ThrowArgumentOutOfRangeException() +72
   System.Collections.Generic.List`1.get_Item(Int32 index) +34
   CallSite.Target(Closure , CallSite , Object , Int32 ) +185
   ASP._Page_Views_Main_Index_cshtml.Execute() in e:\Path\To\Project\Views\Main\Index.cshtml:6
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
4

0 回答 0