I’m learning Linq and MVC2 I’m making progress, but I’m a little stuck on how I should get a page descrption into a View that uses a collection.
In my controller I have the following construct
public ActionResult Group(int id)
{
var result = (from d in db.ErrorDetails.Where(r => r.ErrorTerminal.ErrorTerminalGroupID == id)
orderby d.ErrorTerminal.TerminalNumber ascending, d.ErrorRecordedAt descending
select d);
return View(result);
}
and on my view
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>????? how to get a title from the 1st record?????</h2>
<table>
<%
foreach (var item in Model) {
%>
//code for setup of the rows etc
<% }%>
</table>
How do I get the Title from the Linq query that I made, OR do I need to make a ViewModel to house that?
Please do not pass
IEnumerable<T>s to your view. Instead consider using aList<T>and do the first-time enumeration in the controller. I only recommend this pattern for ViewModels because you can catch the exception of the first-time enumeration in your controller code and not let the view rendering die horribly and result in partial HTML output because the controller screwed up and passed the view something it could not fully enumerate over.That being said, the best approach is to use a ViewModel and have it house properties
string TitleandList<ErrorDetail> ErrorDetails.