我不明白爲什麼我的應用程序崩潰。這是我在控制器的行動:嘗試訪問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
時,它崩潰。即使我試圖只在調試器中進行。
您將需要轉換它('ViewBag'是動態的) - '名單階段= ViewBag.Stages如清單',你將需要測試'空'因爲你的控制器只把它設置在'if'塊中。 –
@StephenMuecke,它不需要轉換,如果它是'null',那麼這個代碼必須使'stages'等於'null',不會崩潰應用程序。但它不是'null',因爲我在我的控制器中檢查過它。 –
您是否嘗試過在Global.asax中的Application_Error()中捕獲錯誤? –