tags:

views:

205

answers:

1

On my index/list view I have a list of records from the database, currently they display the foreign key id, I would like to change this so that it displays the text name column of the reference table.

I can't seem to find any examples on this, am I missing something really simple?

+1  A: 

In your view, instead of saying:

<%= Html.Encode(Model.ID) %>

Say:

<%= Html.Encode(Model.MyFriendlyName) %>

If you are looking up the name from the foreign table, and you are using an ORM like Linq to SQL, the most likely possibility is something like this:

<%= Html.Encode(Model.MyForeignTable.MyFriendlyName) %>

Of course, I am making a lot of assumptions. If you can provide more detail as to what is in your View, Controller, and Model, I can be more precise.

Robert Harvey
for example: Project.Client.ClientName exists but is null. Project.ClientID exists but Project.ClientName does not exist.Do I need to bind Project.Client.ClientName somehow?
Daniel Draper
What kind of model are you using? Linq to SQL?
Robert Harvey
Yeah I am using Linq to SQL
Daniel Draper
Do you have an association in the Linq to SQL designer between the two tables (ClientID to ClientID)?
Robert Harvey
I just created a database diagram when I added the database to the app_data folder and setup a relationship one to many.
Daniel Draper
Just looked at the dbml file and yes there is a relationship there.
Daniel Draper
Thanks fixed the association as it was one to one :)
Daniel Draper