I'll just jump right into it. I have a model called "Request" which I created using LINQ to SQL. I have another model called "Comment". A "Request" can have many "Comments".
In my controller I have this code:
public ViewResult Details(int? id)
{
return View(requestService.GetRequest(id));
}
In my view I have this markup (edited for brevity)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.Request>" %>
<p>
CustomerNumber:
<%= Html.Encode(Model.CustomerNumber) %>
</p>
<p>
CustomerName:
<%= Html.Encode(Model.CustomerName) %>
</p>
... etc ...
<table id="comments">
<thead>
<tr>
<th>Date</th>
<th>User Name</th>
<th>User IP</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<% foreach (var comment in Model.Comments) { %>
<tr>
<td><%= Html.Encode(comment.CommentDate) %></td>
<td><%= Html.Encode(comment.CommentUserName) %></td>
<td><%= Html.Encode(comment.CommentIP) %></td>
<td><%= Html.Encode(comment.Comment1) %></td>
</tr>
<% } %>
</tbody>
</table>
Ok, here's what's happening. When I call the Details method/action, it uses my service layer to go get the request that the user wants to see just fine. However, when it goes to loop over the Comments associated with the Request, it throws an "Invalid Cast Exception" and dies. This code was working 100% correct two days ago, and no changes were made to this portion of the project (other files, but not this stuff).
I have tried to find the casting problem and I'm at a loss. Please help me, as I think this is my last obstacle to overcome before my project can really take off. If you need anymore information, let me know.