2016-05-07 102 views
0

私は以下のような問題があります:
私は3つのテーブルMenuFront、NewsとSubNewsを持っています。 MenuFrontテーブルにはリンクのリストが含まれています。次に、Newsには、各種類のコンテンツが含まれます&は、MenuFrontで外部キーを持っています。
ニュースのいくつかの時間彼らはリンクとして保存し、SubNewsに詳細が保存されます。その後、ニュースを検索するためのクエリを書きますが、SubNews情報は取得できません。 これは私が入力したときにビュー内の外部キーデータを表示MVC

public partial class Loyalty_News 
{ 
    public Loyalty_News() 
    { 
     this.Loyalty_SubNews = new HashSet<Loyalty_SubNews>(); 
    } 

    public int Id { get; set; } 
    public Nullable<int> CategoryId { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public string Content { get; set; } 
    public string ImagePath { get; set; } 
    public Nullable<int> LikeCount { get; set; } 
    public Nullable<byte> IsHot { get; set; } 
    public Nullable<byte> Status { get; set; } 
    public Nullable<System.DateTime> CDate { get; set; } 
    public string LUser { get; set; } 
    public Nullable<System.DateTime> LDate { get; set; } 
    public int MenuFrontID { get; set; } 

    public virtual Loyalty_MenuFront Loyalty_MenuFront { get; set; } 
    public virtual ICollection<Loyalty_SubNews> Loyalty_SubNews { get; set; } 
} 

が続いてここで、その後

if (SubMenu != "" && SubMenu != null && Menu != null && Menu != "") 
     { 
      var news = from a in db.Loyalty_News 
         join b in db.Loyalty_SubNews on a.Id equals b.NewsId 
         where a.Id == Int32.Parse(Menu) && b.NewsId == Int32.Parse(SubMenu) 
         select a; 
      if (fromdate != null) 
      { 
       news = news.Where(m => m.CDate >= fromdate); 
      } 
      if (todate != null) 
      { 
       DateTime newtodate = todate ?? DateTime.Now; 
       news = news.Where(m => m.LDate <= System.Data.Entity.DbFunctions.AddDays(newtodate, 1)); 
      } 
      return View(news); 
     } 
     else 
     { 
      var news = from a in db.Loyalty_News 
         join b in db.Loyalty_SubNews on a.Id equals b.NewsId 
         select a; 
      if (Menu != null && Menu != "") 
      { 
       news = news.Where(m => m.Id == Int32.Parse(Menu)); 
      } 
      if (fromdate != null) 
      { 
       news = news.Where(m => m.CDate >= fromdate); 
      } 
      if (todate != null) 
      { 
       DateTime newtodate = todate ?? DateTime.Now; 
       news = news.Where(m => m.LDate <= System.Data.Entity.DbFunctions.AddDays(newtodate, 1)); 
      } 
      return View(news); 
     } 

とビューで

をクエリのコントローラーである

@item.Loyalty_SubNews. 

それはSubNewsの列が、今、それのみを表示する方法を示す必要があります(例:どこに、連合、ToList、....)

私は蘇のデータを取得するのを手伝ってくださいbNews。

答えて

1

このコードを試してみては

@foreach (var item in Loyalty_SubNews) //because it is list you can't get properties directly, you need to return in loop 
     { 
    <p> @item.property </p> // now here the properties of Loyalty_Subnews will appear 

} 
+0

感謝仲間をスニペットが、それは私の問題では動作しません:( –

関連する問題