views:

43

answers:

2

I'm building out a simple simple ASP.NET MVC 2 page. Was able to do the Edit page the way I want, but am having some trouble with the Index page.

I have a table tblContacts that is joined by another table tblStates, database is normalized so the contact table (name, phone, address, city, etc) has a StateId which references a tblStates lookup table which consists of tblStates.Id and tblStates.Name.

Using Linq-To-Sql at this point... I have the Index page showing currently:

public ActionResult Index() { return View(_repository.ListAll()); }

While we have the contacts data in its glory, am not sure how do I display the tblState.Name in the view?

<%: item.StateID %>  <-- how shall I change this into tblState.Name?

In the Edit form I was able to display the tblStates.Name by doing this:

<%: Html.DropDownList("States", (IEnumerable<SelectListItem>)ViewData["StateID"])%>

How do I do something like the above and display the label the 'right' way? I know there's something simple and there's a keyword/sample that I'm not finding. All the code samples I've found are telling me how to find child records and loop through it. Looked at SingleOrDefault(..), FirstOrDefault(..) and it doesn't seem right.

Thanks.

A: 

If the association between tblContacts and tblStates is properly set in L2S you should be able to do the following:

public ActionResult Index() { return View( _repository.tblContacts ); }

and then

<%: item.tblState.Name %>
Fabian
A: 

Sorry for the delay, sidetracked with other projects.

Anyway, the above proposed solution didn't work. Went with EF4 and used a partial class to extend the tblContacts to return the State's Name... let me know if you want more info.

rob