0

Edit: The reason this error occurred was that in my _Layout.cshtml, I had written a link that referred to this actionmethod as Html.Action() instead of Html.ActionLink()...

I am building a website in MVC5 with EF6.1.3. I get the following exception when passing a collection of Project to a view:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

The ActionMethod gets called an infinite amount of times.

I searched around and it appears that I get a reference loop in the Json serializer. So I added [JsonIgnore] to the fields in Project that has a reference back to Project. I also added the following to Global.asax:

HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter
    .SerializerSettings
    .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

Still no difference. I also tried to set the navigation properties that references back to Project to null, still no difference. That makes me think that maybe there is something else going on.

Code that generates error:

public ActionResult Index()
        {

            return View(db.Projects.Include(p => p.ProjectMissingPeople).ToList());
        }

Project.cs:

public class Project
    {
        public int ProjectID { get; set; }
        public string ProjectName { get; set; }
        public string ProjectDescription { get; set; }
        public string ProjectImageUrl { get; set; }

        [JsonIgnore]
        public string FounderId { get; set; }
        [ForeignKey("FounderId")]
        [JsonIgnore]
        public ApplicationUser Founder { get; set; }
        public ICollection<ProjectMissingPeople> ProjectMissingPeople { get; set; }

    }

ProjectMissingPeople.cs:

public class ProjectMissingPeople
{
    public int ProjectMissingPeopleID { get; set; }
    public string Text { get; set; }
    public bool Open { get; set; }
    [JsonIgnore]
    public int ProjectID { get; set; }
    [JsonIgnore]
    public Project Project { get; set; }      

}

Index.cshtml:

@model IEnumerable<ProjectStockholm.BusinessModels.Project>

@foreach(var project in Model)
{
    @Html.Partial("_ProjectThumbnail", project)
}

ProjectThumbnail.cs:

  @model ProjectStockholm.BusinessModels.Project

    <div class="project-thumbnail">
        <img url="@Model.ProjectImageUrl" />
        <h2>@Model.ProjectName</h2>
        <p>@Model.ProjectDescription</p>
        <h4>Open positions</h4>
        @foreach (var missingPeople in Model.ProjectMissingPeople)
        {
            <label>@missingPeople.Text</label>
            <button class="btn btn-project-join-small">JOIN</button>
        }
    </div>

Do you know how to help me?

hellogoodnight
  • 1,989
  • 7
  • 29
  • 57
  • JSON formatting is not the problem here, so the `[JsonIgnore]` et al. won't help :/ – juunas Oct 04 '17 at 16:32
  • If you debug the project and put `db.Projects.Include(p => p.ProjectMissingPeople).ToList()` on a separate line, can you hit a breakpoint on either line in the action method? – juunas Oct 04 '17 at 16:33
  • If you are using Razor, it shouldn't be using Json to deserialize anything so I think something else is in an infinite loop. Or maybe it is crashing on the Razor and it just keeps getting rerouted to the same url over and over again? – Daniel Lorenz Oct 04 '17 at 16:41
  • You guys are right. This is not something with JSON. I cleared the View to just contain an empty div tag, and not taking the model as argument. Commented out the line in actionmethod that querys db. Same result! WTF! I don't get it. – hellogoodnight Oct 04 '17 at 17:50
  • [Use a view model](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) – adiga Oct 04 '17 at 19:11
  • Do you have a @Partial call that is getting into an infinite loop? You may want to post your Razor code, I think the error will be in there somewhere. – Daniel Lorenz Oct 05 '17 at 19:47
  • @DanielLorenz , see edit on top. Thanks for taking your time – hellogoodnight Oct 06 '17 at 09:12
  • I don't think I see enough information. What I would do is remove all code from the main cshtml page and see if it loads. Then, part by part, keep adding code back in until it crashes so you can figure out which part is crashing. If it crashes on a blank page, then _Layout may have a problem. – Daniel Lorenz Oct 06 '17 at 13:06

0 Answers0