views:

59

answers:

1

I am starting asp.net MVC2 by going through a few tutorials, but they are all too basic when trying to apply this to my real world problem.

I have been following a tutorial based on a simple one table database. I build the data model using ADO.NET Entity Data Model and select to generate the model from database. Then I make a controller for the table, all I do in the controller is list the items, i.e.

private sampleDatabaseDBEntities _dataModel = new sampleDatabaseDBEntities();

public ActionResult Index()
{
     return View(_dataModel.TheOnlyTable.ToList());
}

So I am trying to figure out how to start, when the schema is more complex.

Say I have a FruitShop and have a table like Vegetables that references values from another table (Color for example).

Obviously the auto generated code will not work:

<table>
        <tr>
            <th></th>
            <th>
                SampleID
            </th>
            <th>
                SampleForeignKeyFieldID
            </th>
            <th>
                SampleForeignKeyFieldID
            </th>
            <th>
                SampleOtherValue
            </th>
            ....

because I need to lookups based on the value in SampleForeignKeyFieldID to another table (Colors from Vegetables table for example)

Infact for this table, I tried just doing the return View(_dataModel.Table.ToList()); but nothing displayed at all. I guess because most fields are referenced elsewhere.

How do I get started building the views for these cases? Is there any tutorial/guide you know of that would help get me started?

Thanks

+3  A: 

Best place to learn:

http://www.asp.net/mvc

Views and ViewModels - http://www.asp.net/mvc/tutorials/mvc-music-store-part-2

Try to follow this music store app.

There's Nerd Dinner too:

Free ASP.NET MVC “NerdDinner” Tutorial Now in HTML

Leniel Macaferi
I hope these examples all look at linking with a database bigger than one table ? I've already found plenty of examples for that and it's not helping.
baron
Yes... the pages linked above have what you want.
Leniel Macaferi
I agree with the ViewModels suggestion from Leniel. I would HIGHLY recommend using AutoMapper (http://automapper.codeplex.com/) to make the mapping from your Models to your ViewModels.
Jeff T