amateur
amateur

Reputation: 44605

how to manage logic in the _Layout.cshtml

I am currently working with an mvc site where I have a fairly extensive main layout page. It is dependent on data from the database which in turns includes inherent logic as what to include etc on the layout.

Most of my controller actions are rendered within this layout. I am not sure of how to work this. Being used to master pages in web forms, all the logic resides in the master page. I have a couple mechanisms to achieve the common layout logic but looking for the best practise way of achieving such.

Options are:

Any other options open to me or recommendations?

Upvotes: 2

Views: 1382

Answers (1)

Dave Alperovich
Dave Alperovich

Reputation: 32490

If I understand you correctly, I would use an action partial

@Html.Action("{ActionName}", "{Controller}", new { roleName= "Admin" })

Action Partials call dedicated controller action methods of type

   [ChildActionOnly]
    public ActionResult _TopNav(string roleName)

This way you can design recurring logic that will propagate throughout your application without replicating.

Even better, if your _Layout handles privilege based link generating, you can pass role id's and control what the end user sees and what their navigation buttons point to.

Upvotes: 3

Related Questions