MVC3 Partial Views

前端 未结 3 1966
自闭症患者
自闭症患者 2021-01-15 03:52

Still Learning MVC3, EF. For now I am connecting to MySql but I believe that will not be relevant. For simplicity, I decided to use one database for my test application and

3条回答
  •  旧巷少年郎
    2021-01-15 04:17

    You could do the following:

    Controller:

    public ActionResult Home()
    {
      IEnumerable myData = LinqQueryToGetAllTheDataUnFiltered();
      ViewData.Model = new MyViewData { MyData = myData; }
      return View();
    }
    

    ViewModel class:

    public class MyViewData
    {
      List MyData { get; set; }
      List News { get { return MyData.Where(m => m.Category = "News"); } }
      List Events { get { return MyData.Where(m => m.Category = "Events"); } }
    }
    

    View:

    @model MyViewModel
    
    @Html.Partial("NewsPartial", Model.News)
    @Html.Partial("EventsPartial", Model.Events)
    

    Partial:

    @model IEnumerable
    

    This way we only queried for the data once and just passed a different set to each partial

提交回复
热议问题