Click here to Skip to main content
15,896,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables:
Countries(Country_ID,Country_Name) here Country_ID is primary key
States(State_Id,State_Name,Country_ID) here Country_ID is foreign key

When I design Index page for States: url(http://localhost:2010/States/Index)
StatesController.cs contains this code:

//I am using LINQ and datacontext object with name db and a function db.GetStates() returns list of State objects
C#
index(){
 return view(db.GetStates());
}

Folder Structure of Application is:

Application-View-States-Index.aspx and when I execute this page it shows result as Country_ID (1,India)

State_ID State_Name Country_ID
1        Punjab      1

but I want to show:
State_ID State_Name Country_Name
1        Punjab     India

but model object or item object of model contains only a list of States object which only contains Country_ID instead of Country_Name.

If anybody has any such sample application that using LINQ in Model, MVC and have parent child relationship tables.
Posted
Updated 10-Jun-10 19:27pm
v2

1 solution

I can't reproduce this right now, but I've run into it before.

The way I figured out how to get past it is to do item.Country.Country_Name on the model and run...you should get an error message, which will lead you down the path to correct (it's something to do with LINQ not being referenced properly somewhere.

You might also check the inheritance on the view page (Inherits="") as you might be using something generic.

What version of VS2010 are you using (RC, RTM, a Beta)?

I did this, and could not reproduce:
1) Create a new MVC 2 Web Application
2) Added a database, added the tables above with relationship
3) Created the DBML, added the tables (relationship was there)
4) Compiled the app (to get entities recognized by MVC templates)
5) Added a StateRepository class, added an IEnumerable<state> GetStates() method.
6) Added a StateController
7) Used MVC tooling to create View for States. Strongly typed the view with Models.State, used list as the template.
8) I can then change Country_ID to Country.Country_Name

Cheers,
-jc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900