0

これは私のデータベースです

上記は私のデータベースです。テーブルの連絡先、国、州、都市があります。連絡先テーブルの国は、国テーブルの Id への外部キーです。そして、エンティティ フレームワークを使用してデータを取得しています。データベースを最初に使用しています。次のコードで連絡先の詳細を取得しています。

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

そしてコントローラーの部分では、私はこのようにしています

public ActionResult ViewContacts()
        {

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

最後に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>

私の問題は、それが外国であるため、出力テーブルに CountryId を取得していることです。国名を出力として取得できますか

私が得ている出力は以下のものです

出力

データ部分で国名を取得してコントローラーに渡す方法と、それをビューに渡す方法は? 誰でも私を助けてくれませんか。よろしくお願いします!!!!!!

以下は私の連絡先クラスです

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; }
    }

そしてedmxファイルはここに画像の説明を入力

4

2 に答える 2

3

国を持ってくるようにEFクエリでIncludeあなたのテーブルをしてくださいCountry

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

ビューで以下を使用します

@item.Country.Name
于 2013-10-02T10:45:06.997 に答える
3

国と連絡先テーブルのデータベース関係を設定していると思います。を使用して国名を取得できるようになりました

<td>@item.Country.Name</td>
于 2013-10-02T10:41:24.393 に答える