2017-03-01 45 views
-1

我不明白爲什麼我的應用程序崩潰。這是我在控制器的行動:嘗試訪問ViewBag屬性關閉我的應用程序

public ActionResult AttachmentsPartial(Guid processId) 
{ 
    var staff = GetCurrentStaff(); 
    var process = Db.Processes.Include(nameof(Stages)).FirstOrDefault(p => p.Id == processId); 

    if (process != null && staff != null) 
    { 
     var stages = process.Stages.ToList(); 
     ViewBag.Stages = stages; 
     var files = new List<AttachmentViewModel<Guid>>(); 

     foreach (var stage in stages) 
     { 
      //***Some code*** 
      files.AddRange(items); 
     } 

     return PartialView("_AttachmentsPartial", files); 
    } 

    return PartialView("_AttachmentsPartial"); 
} 

而我的觀點:

@model List<AttachmentViewModel<Guid>> 

@using PM.Models; 
@using PM.Resources.Strings; 
@using PM.Utils; 

@{ 
    List<Stages> stages = ViewBag.Stages; // This crashes here 
} 

早些時候,它StackOverflowException崩潰,但現在輸出什麼,只是完成的應用程序。
我嘗試調試此代碼,當我嘗試訪問ViewBag.Stages時,它崩潰。即使我試圖只在調試器中進行。

+1

您將需要轉換它('ViewBag'是動態的) - '名單階段= ViewBag.Stages如清單',你將需要測試'空'因爲你的控制器只把它設置在'if'塊中。 –

+0

@StephenMuecke,它不需要轉換,如果它是'null',那麼這個代碼必須使'stages'等於'null',不會崩潰應用程序。但它不是'null',因爲我在我的控制器中檢查過它。 –

+1

您是否嘗試過在Global.asax中的Application_Error()中捕獲錯誤? –

回答

1

這是一個函數,我經常用它來查找錯誤,當加載partials等似乎只是轉儲出來沒有痕跡。

在Global.asax.cs中創建:

protected void Application_Error() 
{ 
    Exception exception = Server.GetLastError(); 

    Response.Clear(); 
    Server.ClearError(); 

    HttpException ex = exception as HttpException; 

    var test = ex.GetHttpCode(); 
} 

我只是把一個斷點異常並在它懸停時,它拋出。它通常告訴我出了什麼問題。

1

您嘗試將列表分配給列表?

List<Stages> stages = ViewBag.Stages; 

使其

Stages[] stages = ViewBag.Stages; 
+0

沒有)我複製了舊的來源。只是我試圖將它切換到一個數組,但它仍然無法工作。 –

+0

我固定的來源。 –

相關問題