开发者

Razor _Layout.cshtml common section from a specific 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("_Best开发者_如何学运维SellersPartial", 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜