MVC nested views and partial views

后端 未结 4 1851
死守一世寂寞
死守一世寂寞 2021-01-15 11:34

I am in the process of creating a prototype project using MVC 3 and I have come across a situation which I can\'t seem to find an answer for and it seems like I might be app

4条回答
  •  自闭症患者
    2021-01-15 12:03

    You can not use @RenderBody() multiple times. One @RenderBody() in your main _Layout file is fair enough. In your second view use instead @RenderPartial() or @RenderAction.

    UPDATE (based on first comment)

    Let's say you want to render /Administrator/TheAction, so you will call

    @{
        Html.RenderAction("TheAction", "Administrator");
    }
    

    TheAction action will look like this:

    public PartialViewResult TheAction() {
        return PartialView();
    }
    

    And it will render the view in ~/Views/Administrator/TheAction.cshtml right inside the place from which you called the RenderAction().

    The importance is that it does not accomplish another @RenderBody. As you can see in TheAction() example, you are returning PartialViewResult, which does not have any @RenderBody() helper.

提交回复
热议问题