Komyg Komyg - 2 months ago 10
ASP.NET (C#) Question

ASP .NET MVC - Cannot nest foreach loops in view to display lists

I am creating a website using ASP.NET MVC and I would like to display a list of objects that I retrieve from the database and another list for each object that was retrieved.

Here is my model:

public class JournalViewModel
{
public JournalViewModel()
{
Issues = new List<Issue>();
}

public int Id { get; set; }

[Required]
public string Title { get; set; }

[Required, DataType(DataType.MultilineText)]
public string Description { get; set; }

public string FileName { get; set; }

public string ContentType { get; set; }

public byte[] Content { get; set; }

[Required, ValidateFile]
public HttpPostedFileBase File { get; set; }

public int UserId { get; set; }

[Required, DataType(DataType.MultilineText)]
public string IssueDescription { get; set; }

[Required]
public string IssueTitle { get; set; }

[Required]
public int IssueNumber { get; set; }

public int IssueId { get; set; }

public ICollection<Issue> Issues { get; set; }
}


My controller:

public ActionResult Index()
{
var userId = (int)_membershipService.GetUser().ProviderUserKey;

List<Journal> allJournals = _journalRepository.GetAllJournals(userId);
var journals = Mapper.Map<List<Journal>, List<JournalViewModel>>(allJournals);
return View(journals);
}


And my view:

@model List<Journals.Model.JournalViewModel>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Style
{
<style type="text/css">
.table {
table-layout: fixed;
}

.table td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
}

<h2>Publisher - List of Journals</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>

@if (Model.Count == 0)
{
<div> Click @Html.ActionLink("here", "Create") to publish your first Journal.</div>
}
else
{

<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>File</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink(item.FileName, "GetFile", new { Id = item.Id }, new { target = "_blank" })
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
@foreach (var issue in item.Issues)
{
<tr>
<td>
@Html.DisplayFor(i => issue.IssueTitle)
</td>
</tr>
}
}
</tbody>
</table>
}


This view used to work fine until I added this code:

@foreach (var issue in item.Issues)
{
<tr>
<td>
@Html.DisplayFor(i => issue.IssueTitle)
</td>
</tr>
}


And now it has stopped working. What I am doing wrong?

Also, is there a way to retrieve an error message from this view?

Answer

Remove @ before the nested foreach cycle. You have to also review you app and see where standard ASP.NET MVC exception was suppressed. Most possible places are web.config and Global.asax.cs files. This will allow you to get more details about errors which occur in your application.