在视图MVC

时间:2016-05-07 08:04:29

标签: c# asp.net-mvc

我遇到如下问题:
我有3个表MenuFront,新闻和子新闻。 MenuFront表包含链接列表。然后在新闻中将包含各种内容和在MenuFront上有外键。
有时在新闻中它们存储为链接,然后细节将存储在SubNews中,然后当我编写查询来查询新闻但无法获取SubNews信息时。 这是控制器

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的列,但现在它只显示方法(例如:Where,Union,ToList,....)

请帮助我获取SubNews的数据。

1 个答案:

答案 0 :(得分: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

}