Rendering partial views with Razor in MVC5

前端 未结 3 1083
误落风尘
误落风尘 2021-02-12 09:52

I\'m trying to get a partial view to render using Razor in MVC5. When I use

@{ Html.RenderPartial(\"ViewName\", model); }

I get the parser erro

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 10:43

    You haven't posted the context of that code, but that error only really happens when you're using @ directly within a code block without any HTML wrappings. For example:

    @if (true) {
        @{ Html.RenderPartial(...); }
    }
    

    Would give you the error, while:

    @if (true) {
        
    @{ Html.RenderPartial(...); }
    }

    Would be fine. You could also solve it by just removing the code block for Html.RenderPartial entirely, including the @:

    @if (true) {
        Html.RenderPartial(...);
    }
    

提交回复
热议问题