tags:

views:

22

answers:

1

Simply put: My model has two tables, Concepts and Transactions, which gives birth to two controllers of the same name. Transactions has a foreign key pointing to the primary key of Concepts (so you have a transaction like "$200 spent yesterday on concept 1", and concept "1" being "gas"). In the Transactions Index view, I want to show, instead of the concept ID behind the transaction, the description of the concept.

In plain Web Forms, I would do a look up to the DB and done. But in MVC, what would be the recommended way to do it?

Here's a snippet of my Transactions Index view:

    <% foreach (var item in Model) { %>

    <tr>
        <td>
            <%: Html.ActionLink(Resources.ActionEdit, "Edit", new { id=item.Id }) %> |
            <%: Html.ActionLink(Resources.ActionDetails, "Details", new { id=item.Id })%> |
            <%: Html.ActionLink(Resources.ActionDelete, "Delete", new { id=item.Id })%>
        </td>
        <td>
            <%: item.Id %>
        </td>
        <td>
            <%: String.Format("{0:g}", item.Date) %>
        </td>
        <td>
            <%--**DISPLAY THE CONCEPT DESCRIPTION FOR THIS item.ConceptId HERE** --%>
            <%--<%: item.ConceptId %>--%>
        </td>

Thanks!

A: 

If you are using Linq to SQL (or any similar ORM), you should already have support for:

<%: item.Concept.Description %>

...That is, if your ORM provides support for relationships between the database tables.

Robert Harvey
Yes, thanks... I was not expecting item to contain the foreign reference... apologies for the easy question ☺
Ariel