madhu goud
madhu goud

Reputation: 27

How to bind data to layout page in asp mvc 4

how to write controller code to shared layout page,

i want to get menu from database to layout,

am able to get menu in normal view page,

this is my layout page

<pre lang="html"> <div style="width:1230px">
                    <script type="text/ng-template" id="treeMenu">
                        <a href="{{menu.Description}}">{{menu.Name}}</a>

                        <ul ng-if="(SiteMenu | filter:{ParentID : menu.Id}).length > 0">

                            <li ng-repeat="menu in SiteMenu | filter:{ParentID : menu.Id} : true" ng-include="'treeMenu'"></li>
                        </ul>
                    </script>
                    <ul class="main-navigation">
                        <li ng-repeat="menu in SiteMenu | filter:{ParentID : 0} : true" ng-include="'treeMenu'"></li>
                    </ul>

Upvotes: 1

Views: 890

Answers (1)

Wahid Bitar
Wahid Bitar

Reputation: 14084

You should isolate this in Partial and use RenderAction in your layout page

Steps in nutshell :

  1. Create Action in some controller "let say CommonController" called SiteMenu for example.
  2. Put the code that will bring the Data from Database and map it to suitable ViewModel to represent your Menu items.
  3. The Action should return PartialView instead of View.
  4. Consider this Action regardless of your main layout. So just create a special view for it and put the logic of Html in that view.
  5. In suitable place in your main layout just use @Html.RenderAction("SiteMenu ","Common")

for more information read about RenderPartial vs RenderAction vs Partial vs Action in MVC

Upvotes: 1

Related Questions