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
This is wrong:
@Html.RenderPartial("ViewName", model);
This is correct:
@{ Html.RenderPartial("ViewName", model); }
The parsing error might be caused by the partial view's content. For example, if you have an email address, make sure you use @@ to properly escape the @ sign.
Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.
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) {
<div>
@{ Html.RenderPartial(...); }
</div>
}
Would be fine. You could also solve it by just removing the code block for Html.RenderPartial
entirely, including the @
:
@if (true) {
Html.RenderPartial(...);
}
You may also use @Html.Partial("~/View/Home/myview.cshtml")
It returns string while Html.RenderPartial
calls Write internally, and returns void.