John John
John John

Reputation: 1

How to call an action method from my _layout view

I have an action method that display the latest 5 records inside my application :-

[HttpGet]
public IEnumerable<Technology> LatestAssets()
{
      var tech = repository
                          .LatestTechnology()
                          .OrderByDescending(a => a.TechnologyID).ToList() ;
      return tech;    
}

But how I can call this action method when the _layout view is rendered (without having to click on any link), and iterate over it an display the information regarding the latest 5 records ? my _layout view have the following section to display the latest record info under it:-

<li class = "nav-header hidden-tablet"
     style = "background-color:#3E9BD4 ; color:white">Latest Assets
</li>

Upvotes: 2

Views: 3234

Answers (1)

WannaCSharp
WannaCSharp

Reputation: 1898

You can try this:

Controller:

[HttpGet]
public ActionResult LatestAssets()
{
    var tech = repository
                        .LatestTechnology()
                        .OrderByDescending(a => a.TechnologyID).ToList();
    return PartialView("_Assets", tech);

}

_Assets(PartialView):

@foreach (var asset in Model)
{
     <li class = "nav-header hidden-tablet"
         style = "background-color:#3E9BD4 ; color:white">@asset
    </li>
}

Then you can call it in your layout view with this

EDIT :

Layout View:

@{ Html.RenderAction("LatestAssets", "ControllerName"); }

Upvotes: 9

Related Questions