2

I have a controller action:

 public ActionResult Sub(int id)
    {
      SubPage_Table subpage_table = db.SubPage_Table.Single(s => s.PageID == id);
        if (subpage_table == null)
        {
            return HttpNotFound();
        }
        return View(subpage_table);
    }

And a View:

@model Name.Models.DB.SubPage_Table
@Html.Raw(HttpUtility.HtmlDecode(Model.PageContent))  

But my layout needs to be strongly typed:

@model IEnumerable<Name.Models.DB.SubPage_Table> 
          @foreach (var item in Model)
          {@: <li> @Html.ActionLink((item.PageTitle), "Sub", new { id = item.PageID })</li>

}

How do I populate my view and Layout with data?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
user3586498
  • 45
  • 10
  • You are going to assume that every single page you ever create with that layout in your application needs `IEnumerable`? That seems very unrealistic. – Erik Philips Apr 29 '14 at 18:38
  • Hm, I wanted to... Unless You know how to create links like @Html.ActionLink((item.PageTitle), "Sub", new { id = item.PageID } not using Ienumerable? Beacuse for now I create using foreach loop – user3586498 Apr 29 '14 at 18:44
  • So your real question sounds like *how do I retrieve data for my layouts*? – Erik Philips Apr 29 '14 at 18:52
  • Hm, Lokoks like you are right. I tried with List subpage_table =db.SubPage_Table.Where(s => s.PageID == id).ToList(); in controller but ewentually as a result in new { id = item.PageID } Pages I got only one link displaying instead of all. (only the current one was displayed so if I went to the SubPage "Test" then in Menu only "Test" was displayed, instead of "test2" , "test3" etc... – user3586498 Apr 29 '14 at 19:10
  • I've updated your question to talk about the relevant information. – Erik Philips Apr 29 '14 at 19:13

1 Answers1

2

Here is on of the better options:

Create a partial view with your data-driven information

public LayoutController
{
  public ActionResult CreateMenu()
  {
    var model = db.getmenudata();

    return PartialView(model);
  }
}

Then your Layout/Createmenu.cshtml creates just the html that is data driven.

In your _Layout.cshtml:

<html>
... etc

@Html.RenderAction("Createmenu", "layout");

... etc

@RenderBody();


... etc
</html>
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
Erik Philips
  • 53,428
  • 11
  • 128
  • 150