视图中的多个循环mvc

时间:2013-04-10 09:23:26

标签: asp.net-mvc-4

如果我有以下代码,是否可以在一个视图上有两个视图数据? 我该怎么做呢 提前谢谢了 碎甲弹

public ActionResult Index(long id = 0)
        {
            var contentPage = (from c in db.Tble_content
                               where c.id == id
                               select c);
            var contentlist = (from c in db.Tble_content
                                       where c.EN_TopPageID == id
                                       select c);
            return View();
  }

1 个答案:

答案 0 :(得分:1)

多一点代码会有所帮助。但假设您的Tble_content有这样的结构:

public class Tble_content {
    public int Id {get;set;}
    public string Content{get;set;}
}

你可以拥有这样的视图模型:

public class ContentViewModel {
    public string ContentPage {get;set;}
    public string ContentList {get;set;}
}

然后将其传递给这样的视图:

public ActionResult Index(long id = 0)
{
    var contentPage = (from c in db.Tble_content
                        where c.id == id
                        select c);
    var contentlist = (from c in db.Tble_content
                                where c.EN_TopPageID == id
                                select c);

    return View(new ContentViewModel {
       ContentPage = contentPage,
       ContentList = contentlist
    });
}