0

如果我有以下代码,是否可以在一个视图上拥有两个视图数据?我将如何做这件事提前谢谢

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();
  }
4

1 回答 1

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
    });
}
于 2013-04-10T09:29:41.757 回答