来自特定控制器的 Razor _Layout.cshtml 公共部分

发布于 2024-11-07 10:37:23 字数 2135 浏览 0 评论 0原文

我正在开发我的第一个 ASP.NET MVC 应用程序,我想要完成的是在每个页面的一个部分中显示一个公共列表。就我而言,我有一个 _Layout.cshtml,它有页眉、页脚、主区域和左侧边栏,我希望始终在其中显示从数据库检索的项目列表。

如果我做类似的事情:

@RenderSection("BestSellingFlavors")

在 _Layout.cshtml 中,我可以让任何特定视图在那里显示其“BestSellingFlavors”部分,但就我而言,这是从数据库检索的标准列表 - 我希望始终显示在侧边栏上,无论用户正在查看哪个页面。有道理吗?

目前,我有一个控制器/模型/视图,它提供了我们库存中最畅销口味的视图,但我不确定如何检索和显示该信息,而无需在每个控制器/视图中复制一堆代码。

一个想法是使用 BaseController 来处理最畅销商品的检索。像这样的事情:

public abstract class BaseController : Controller
{
   public PartialViewResult BestSellers()
   {
      try
      {
         var db = IceCreamDBData();
         var all = db.Sales.AsEnumerable();
         var bestsellers = from a in all select new {a.Name, a.UnitsSold};
         return PartialView("BestSellers", bestsellers);
      }
      catch (Exception)
      {
          throw;
      }
   }
}

我的各种控制器将继承 BaseController。 但后来我一直想知道它实际上是如何被调用的,以及视图代码驻留在哪里,@foreach 该数据集合并显示它。这让我觉得我错误地解决了这个问题。我应该如何解决这个问题?


更新: JW 的解决方案和链接让我开始了,现在我(大概)走在正确的轨道上。

在我的 _Layout.cshtml 中,我创建了一个 div:

<div id="BestSellers">
   @Html.Action("BestSellers")
</div>

然后我在共享文件夹中创建了一个名为 _BestSellersPartial.cshtml 的部分视图,其中包含如下内容:

@model HometownIceCream.Models.BestSellersViewModel
<h3>Best Sellers</h3>
@foreach (var item in Model.Entries)
{
   <div>@item.Name</div>
}

然后我的 BaseController 看起来像这样:

public abstract class BaseController : Controller
{
   public PartialViewResult BestSellers()
   {
      try
      {
         var db = IceCreamDBData();
         var all = db.Sales.AsEnumerable();
         var bestsellers = from a in all select new {a.Name, a.UnitsSold};
         BestSellersViewModel mod = new BestSellersViewModel() {Entries = bestsellers};
         return PartialView("_BestSellersPartial", mod);
      }
      catch (Exception)
      {
          throw;
      }
   }
}

这似乎工作得很好。我需要为我的控制器做的唯一一件事就是让它们继承 BaseController 而不是 Controller

I'm developing my first ASP.NET MVC app and what I'd like to accomplish is to display a common list in a section on each page. In my case, I've got a _Layout.cshtml which has a header, footer, main area and a left sidebar where I'd like to always display a list of items retrieved from a DB.

If I do something like:

@RenderSection("BestSellingFlavors")

in the _Layout.cshtml, I can have any particular view display its "BestSellingFlavors" section there, but in my case, this is a standard list retrieved from a database - something I want always displayed on the sidebar, regardless of which page the user is viewing. Make sense?

Currently, I've got a controller/model/view that provides a view of the bestselling flavors in our inventory but I'm not sure how to have that information retrieved and displayed without duplicating a bunch of code in each controller/view.

One idea was a BaseController that handled retrieving the best sellers. Something like this:

public abstract class BaseController : Controller
{
   public PartialViewResult BestSellers()
   {
      try
      {
         var db = IceCreamDBData();
         var all = db.Sales.AsEnumerable();
         var bestsellers = from a in all select new {a.Name, a.UnitsSold};
         return PartialView("BestSellers", bestsellers);
      }
      catch (Exception)
      {
          throw;
      }
   }
}

My various controllers would inherit BaseController.
But then I'm stuck wondering how this actually gets called and where the view code resides that would @foreach that collection of data and display it. This makes me think I"m attacking the problem incorrectly. How should I be solving this?


UPDATE:
J.W.'s solution and link got me started and now I am (presumably) on the right track.

In my _Layout.cshtml I created a div:

<div id="BestSellers">
   @Html.Action("BestSellers")
</div>

then I created a partial view in the Shared folder called _BestSellersPartial.cshtml that has something like this:

@model HometownIceCream.Models.BestSellersViewModel
<h3>Best Sellers</h3>
@foreach (var item in Model.Entries)
{
   <div>@item.Name</div>
}

And then my BaseController looks like this:

public abstract class BaseController : Controller
{
   public PartialViewResult BestSellers()
   {
      try
      {
         var db = IceCreamDBData();
         var all = db.Sales.AsEnumerable();
         var bestsellers = from a in all select new {a.Name, a.UnitsSold};
         BestSellersViewModel mod = new BestSellersViewModel() {Entries = bestsellers};
         return PartialView("_BestSellersPartial", mod);
      }
      catch (Exception)
      {
          throw;
      }
   }
}

And that seems to work quite well. The only thing I needed to do for my controllers was have them inherit BaseController rather than Controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-11-14 10:37:23

我认为 Html.RenderAction 是您所需要的。您可以使用此方法创建共享部分,例如菜单。

I think Html.RenderAction is what you need. You can create a shared section such as menu using this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文