简体   繁体   English

MVC3部分视图

[英]MVC3 Partial Views

Still Learning MVC3, EF. 仍在学习MVC3,EF。 For now I am connecting to MySql but I believe that will not be relevant. 目前,我正在连接MySql,但我相信这将不相关。 For simplicity, I decided to use one database for my test application and I have included a category to differentiate the data. 为简单起见,我决定为测试应用程序使用一个数据库,并且添加了一个类别来区分数据。 For eg I have a news, events,info and pages categories. 例如,我有一个新闻,事件,信息和页面类别。 Now when it comes to listing contents in views for example at the homepage, I want to list latest 5 news items(news category), latest 5 events(events category), welcome text(info category). 现在,例如要在主页上的视图中列出内容,我想列出最新的5个新闻项(新闻类别),最新的5个事件(事件类别),欢迎文本(信息类别)。 i have been able to create partialViews to list these at the different sections of the homepage. 我已经能够创建partialViews,以在首页的不同部分列出这些视图。 But I feel am doing this wrongly since in each of these PartialViews I am querying the same table over and over and just filtering with where cat=.... in the LINQ query. 但是我感觉做错了,因为在这些PartialViews中的每一个我都在一遍又一遍地查询同一张表,并且只使用LINQ查询where cat=....进行过滤。 Can you please confirm if that should be the practice or there is a better way to do this. 您可以确认是否应该这样做,或者有更好的方法来做到这一点。

You could do the following: 您可以执行以下操作:

Controller: 控制器:

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

ViewModel class: ViewModel类:

public class MyViewData
{
  List<MyDataRecords> MyData { get; set; }
  List<MyDataRecords> News { get { return MyData.Where(m => m.Category = "News"); } }
  List<MyDataRecords> 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<MyDataRecord>

This way we only queried for the data once and just passed a different set to each partial 这样,我们只查询一次数据,并向每个部分传递不同的集合

For an uncomplicated way of presenting this type of data, what you are doing is fine. 对于显示这种类型数据的简单方法,您正在做的很好。 You should look at the OutputCacheAttribute for any PartialView Method you use on your Controller. 您应该查看在Controller上使用的任何PartialView方法的OutputCacheAttribute

This is pretty inefficient. 这是非常低效的。 But it's good that you noticed this because querying that database is often the bottleneck in any given request. 但是,您注意到这一点很好,因为查询该数据库通常是任何给定请求中的瓶颈。

For starters you should retrieve that data into a dictionary or model and then pass it to the partial views to render similar to what Bassam outlined. 对于初学者,您应该将数据检索到字典或模型中,然后将其传递到局部视图以进行类似于Bassam概述的渲染。 Ideally, this should be taken care of in the Controller to stick to the MVC paradigm and then passed to the main view, which would then pass the appropriate data to the partial views. 理想情况下,应该在Controller中注意这一点,以坚持MVC范例,然后将其传递给主视图,该视图随后将适当的数据传递给部分视图。

Once you get more advanced/familiar with ASP.NET MVC, you can start looking into caching. 一旦对ASP.NET MVC有了更高级的了解,就可以开始研究缓存了。 I'd stay away from caching for right now since it get somewhat tricky if you have data that is rapidly changing since you need to start worrying about updating/synchronizing/etc. 我现在不打算缓存,因为如果您的数据正在快速变化,因为您需要开始担心更新/同步/等,因此它会变得有些棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM