c# - ASP.NET MVC How to use PartialView in Layout? -
i need render menu in layout using partial view (if there better approach, please let me know). i'm doing way (in layout):
@if (user.isinrole("admin")) { @html.partial("adminmenu") }
and how call in controller:
public actionresult adminmenu() { var = _amr.getadminmenu(); return partialview(am); }
so here partial view:
@model ienumerable<digitalhubonlinestore.viewmodels.adminmenuviewmodel> <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownmenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> admin menu <span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownmenu1"> @foreach (var item in model) { <li><a href="@html.displayfor(modelitem => item.menuitemurl)">@html.displayfor(modelitem => item.menuitemname)</a></li> } </ul> </div>
unfortunately it's not working.
you either have return menu collection in parent view, sending in pertinent model partial, or make htmlform, ajaxform or ajax call partial.
@html.partial("partials/adminmenu",model.adminmenuitems)
or
//server public actionresult adminmenu() { var = _amr.getadminmenu(); return json(am,jsonbehaviour.allowget); } //client @using (ajax.beginform("adminmenu","admincontroller", null, new ajaxoptions { onsuccess = "rendersuccess", onfailure = "renderfailure", onbegin = "renderbegin" }, new { id = "frmvieweradminmenu", name = "frmvieweradminmenu" }) ) { ... <script type="text/javascript"> function rendersuccess(ajaxcontext){ /// ajaxcontext whatever comes getadminmenu() } </script> ... }
or
--server render view
public actionresult adminmenurenderview() { return partial("adminmenupartial",getadminmenuitems()); }
--partial adminmenuviewdynamicloader
@using (ajax.beginform("adminmenurenderview","admincontroller", null, new ajaxoptions { onsuccess = "rendersuccess", onfailure = "renderfailure", onbegin = "renderbegin" }, new { id = "frmvieweradminmenu", name = "frmvieweradminmenu" }) ) { ... <div id="divadminmenucontent"></div> <script type="text/javascript"> function rendersuccess(ajaxcontext){ $('#divadminmenucontent').html(ajaxcontent); } </script> ... }
--partial adminmenupartial
@model ienumerable<digitalhubonlinestore.viewmodels.adminmenuviewmodel> <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownmenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> admin menu <span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownmenu1"> @foreach (var item in model) { <li><a href="@html.displayfor(modelitem => item.menuitemurl)">@html.displayfor(modelitem => item.menuitemname)</a></li> } </ul> </div>
Comments
Post a Comment