簡體   English   中英

在 Asp.net MVC4 中訪問 ViewBag 時出現 NullReferenceException

[英]NullReferenceException while accessing ViewBag in Asp.net MVC4

我正在嘗試在我的視圖中訪問ViewBag數據,如下所示:

<span class="small">@ViewBag.BreadCrumb</span>

我從如下代碼發送ViewBag數據:

ViewBag.BreadCrumb = topic.Category.CatName + " / " + topic.Name;
ViewBag.TopicID = id;

在這里, topic是實體。 我正在使用以下方法返回視圖:

return View(topic);

但是,它總是給我一個例外:

Object reference not set to an instance of an object.

在另一個頁面中,它運行良好。 有什么解決辦法嗎?

編輯::

行動准則如下:

Topic topic = db.Topics.FirstOrDefault(t => t.TopicID == id);
ViewBag.Topics = from t in db.Topics where (t.CatID == topic.CatID) select t;
ViewBag.TopicID = id;
ViewBag.BreadCrumb = topic.Category.CatName + " / " + topic.Name;
return View(topic);

編輯::

它顯示以下異常:

An exception of type 'System.NullReferenceException' occurred in App_Web_ieekpj10.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

但在立即窗口中它顯示輸出: ViewBag.BreadCrumb "C Sharp Edit / Name-edited to C 12"

編輯::堆棧跟蹤

[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_Category_NewSection_cshtml.Execute() in e:\OutSourcingStuffs   \OnlineLibrary\src\OnlineLinkLibrary\OnlineLinkLibrary.Web\Views\Category\NewSection.cshtml:12
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +121
System.Web.WebPages.StartPage.RunPage() +63
System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,   TextWriter writer, WebPageRenderingBase startPage) +177
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +762
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +74
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +388
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +72
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +303
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +155
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +184
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +66
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +65
  System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +66
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9688704
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

編輯視圖

@model OnlineLinkLibrary.Models.Section
@{
   ViewBag.Title = "New Section";
}

<div class="panel panel-default">
<div class="panel-heading ">
    <div class="row ">
        <div class="col-xs-6">
            <h4>
                New Section
                <span class="small">@ViewContext.Controller.ViewBag.BreadCrumb</span>
        </h4>
    </div>
    <div class="col-xs-6 ">
        <div class="pull-right"> 
          -- satic htmls are there
        </div>
    </div>
</div>
</div>

如果您 100% 確定ViewBag 不為空,但 Visual Studio 一直指向該行為空,

然后查看下一行代碼是否有錯誤,編譯器有時會感到困惑;)

這可能有助於溺水的開發人員。

我建議檢查主題是否為空。

Topic topic = db.Topics.Where(a=>a.TopicID == id).FirstOrDefault();
if(topic != null)
{
  ViewBag.Topics = from t in db.Topics where (t.CatID == topic.CatID) select t;
}

您的視圖需要一個 OnlineLinkLibrary.Models.Section:

@model OnlineLinkLibrary.Models.Section

但是你提供了一個主題。 除非 Topic 是 OnlineLinkLibrary.Models.Section 的子類,否則它不會工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM