Search code examples
htmlasp.net-mvcrazordynamicsidebar

Dynamically display table in _Layout sidebar


I have a sidebar in _Layout.cshtml in which I want to dynamically display items from another view/model. I'm not very good at database stuff so I need some help.

Sidebar in _Layout.cshtml:

<div id="sidebar">
    @if (IsSectionDefined("SideBar")) {
        @RenderSection("SideBar", required: false)
    } else {
        *Here is where I want the table to be displayed*
    }

The view I want to display in the sidebar is located in ~/Views/Sidebarpics/index.cshtml.

If you need more code to be able to help me just ask!

Thanks

SOLVED, My solution:

I made a duplicate of SidebarPics/index and named it index2.

_Layout Sidebar:

    <div id="sidebar">
        @if (IsSectionDefined("SideBar"))
        {
            @RenderSection("SideBar", required: false)
        }
        else
        {
            @Html.Action("Index2", "SidebarPics")
        }

HomeController:

 public class HomeController : Controller
{
    //
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }
}

SidebarPicsController:

public ActionResult Index()
    {
        return View(db.SidebarPics.ToList());
    }

    [ChildActionOnly]
    public ActionResult Index2()
    {
        return PartialView(db.SidebarPics.ToList());
    }

This might not be the best solution, but it works fine for me and this way it was easy to style them differently.


Solution

  • Use either @Html.Partial or @Html.Action depending on whether you need that view to go through a controller or not.

    If you just want to load the cshtml without needing to using a controller action then render it as a partial view:

    @Html.Partial("~/Views/Sidebarpics/index.cshtml")
    

    If you need to process the view through a controller action then render it as a child action:

    @Html.Action("Index", "SidebarPics")
    

    EDIT: Add Child Controller Example

    EDIT 2: I realized that I had flipped the controller and action parameters. Action should be first.

    EDIT 3: Update to change name of partial index per comments.

    Relevent Layout Page Section:

    <div id="sidebar">
        @if (IsSectionDefined("SideBar")) {
            @RenderSection("SideBar", required: false)
        } else {
            @Html.Action("PartialIndex", "SidebarPics")
        }
    </div>
    

    Home Controller:

     public class HomeController 
     {
          [HttpGet]
          public ActionResult Index() 
          {
               return View();
          }
     }
    

    SidebarPics Controller:

     public class SidebarPicsController 
     {
          public ActionResult Index() 
          {
               return View(db.SidebarPics.ToList());
          }
    
          [ChildActionOnly]
          public ActionResult PartialIndex() 
          {
               return PartialView("Index", db.SidebarPics.ToList());
          }
     }
    

    I believe it is also possible to omit the ChildActionOnly attribute but then you will have an action that only returns a partial view.