MVC: How to get controller to render partial view initiated from the view

余生颓废 提交于 2019-12-03 05:29:06
brinch

Thanks to Stephen Muecke and Erick Cortorreal I got it to work.

This is what the controller should look like:

[ChildActionOnly]
public PartialViewResult GetMenu()
{
   MenuStructuredModel menuStructuredModel = menuBusiness.GetStructuredMenu();

   return PartialView("~/Views/Shared/MenuPartial", menuStructuredModel);
}

And it may called like:

@Html.Action("GetMenu", "Home")

(Hence GetMenu() is declared in the HomeController in my example).

The controller is now called (and the model is populated) prior to the partial view is rendered.

You should use: @Html.RenderAction Or @Html.Action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!