0

This is my database

The above is my database .I have for tables Contact,Countries,States,Cities.The Country in contact table is foreign key to the Id in countries table. And I am using entity framework to get the data.I am using database first approach.I am getting my Contact details by the following code

public class ContactManager
    {
        public static List<Contact> GetContacts()
        {
            using (var obj = new EntityDBConnectionString())
            {
                return obj.Contacts.ToList();
            }
        }
}

And in the Controller part I have like this

public ActionResult ViewContacts()
        {

            return View("ViewContacts",ContactManager.GetContacts());
        }

and atlast the View(cshtml)

@model  List<Entity.Data.Contact>

@{
    ViewBag.Title = "ViewContacts";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ViewContacts</h2>
<table style="font-family:'Segoe UI';font-size:20px;display:table" border="1" cell-spacing="1" >

        @foreach (var item in Model)
        {
             <tr style="border:1px solid black;padding-right:3px;background-color:lightblue;display:table-row">
    <td>@item.Id</td>
    <td>@item.Name</td>
    <td>@item.Mobile</td>
    <td>@item.EmailId</td>   
    <td>@item.Address</td>
    <td>@item.Country</td>
</tr>
        }

</table>

My problem is that I am getting the CountryId's in the output table as it is a foreign.Can I get the Country Name as output

The output I am getting is the below one

Output

How to get the Country name in the data part and pass to the controller and how to pass it to the view? Can anyone help me out .Thanks in advance!!!!!!

The below is my Contact class

public partial class Contact
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string EmailId { get; set; }
        public string Mobile { get; set; }
        public string Address { get; set; }
        public Nullable<int> City { get; set; }
        public Nullable<int> State { get; set; }
        public Nullable<int> Country { get; set; }

        public virtual City City1 { get; set; }
        public virtual Country Country1 { get; set; }
        public virtual State State1 { get; set; }
    }

and the edmx file isenter image description here

Bharat Bhushan
  • 1,175
  • 3
  • 15
  • 24
  • If you have the relationship setup correctly at the DB end EF is clever enough to generate a navigation property for you so you shouldn't even need to query the table separately i.e. `contact.Country.Name`. – James Oct 02 '13 at 10:36

2 Answers2

3

I asume that you have set database realtionship for country and contact table. now in view you can get country name using

<td>@item.Country.Name</td>
Chirag Arvadia
  • 1,190
  • 7
  • 11
3

Please Include your Country table in EF query like to bring the country

 public static List<Contact> GetContacts()
    {
        using (var obj = new EntityDBConnectionString())
        {
            return obj.Contacts.Include("Country").ToList();
        }
    }

and use the below in your view

@item.Country.Name
Murali Murugesan
  • 21,303
  • 16
  • 65
  • 114