asp.net mvc - MVC: How to get controller to render partial view initiated from the view -
in mvc5 project want create menu in partial view. menu dynamic in sense built content in database. have controller taking care of creating menu , returning menu model partial view:
public partialviewresult getmenu() { menustructuredmodel menustructuredmodel = menubusiness.getstructuredmenu(); return partialview("~/views/shared/menupartial", menustructuredmodel); } in partial view called menupartial want use razor iterate on menu items, like:
@model myapp.models.menu.menustructuredmodel <div class="list-group panel"> @foreach (var category in model.viewtypes[0].categories) { <a href="#" class="list-group-item lg-green" data-parent="#mainmenu">@category.shownname</a> } </div> now problem view in insert partial view. if in view do:
@html.partial("menupartial") it won't call controller populate model data first. want let controller return partial. don't know how view. in pseudo code like:
@html.renderpartialfromcontroller("/mycontroller/getmenu")
thanks stephen muecke , erick cortorreal got work.
this controller should like:
[childactiononly] public partialviewresult getmenu() { menustructuredmodel menustructuredmodel = menubusiness.getstructuredmenu(); return partialview("~/views/shared/menupartial", menustructuredmodel); } and may called like:
@html.action("getmenu", "home")
(hence getmenu() declared in homecontroller in example).
the controller called (and model populated) prior partial view rendered.
Comments
Post a Comment