尝试访问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时,它会崩溃。即使我尝试仅在调试器中进行。

这是我经常用来在加载部分时发现错误的函数,似乎没有任何跟踪。

在global.asax.cs中创建:

 protected void Application_Error()
 {
    Exception exception = Server.GetLastError();
    Response.Clear();
    Server.ClearError();
    HttpException ex = exception as HttpException;
    var test = ex.GetHttpCode();
}

我只是在异常上放置一个断点,然后在扔时悬停在它上。通常会告诉我出了什么问题。

您尝试将数组分配给列表?

List<Stages> stages = ViewBag.Stages;

Stages[] stages = ViewBag.Stages;

最新更新