24

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 error:

Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.

When I remove the {}, i.e.:

@Html.RenderPartial("ViewName", model);

I get the compilation error

Cannot implicitly convert type 'void' to 'object'.

What am I doing wrong?

0

6 Answers 6

35

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(...);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well spotted, it was wrapped in a @foreach{}!
nice answer, detail was far better than mine :)
26

You may also use @Html.Partial("~/View/Home/myview.cshtml")

It returns string while Html.RenderPartial calls Write internally, and returns void.

1 Comment

IMHO this is the correct/best answer to the question.
10

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.

2 Comments

it can also be because the code block (pact with the @ sing) is after and other code block (foreach if etc.) if this is the case you should not put an @ in front of the code block
@Believe2014 in my case, there is index view, i have create partial view search form i want to include in index view by @{ Html.RenderPartial("TestOne"); }, if i run TestOne it self it's working but if i run index there is error , The model item passed into the dictionary is of type 'PagedList.PagedList`.....
4

can you show the code surrounding your RenderPartial? I'm guessing you're in a loop, if block, or some other type of code block. If so, you would just call

Html.RenderPartial("ViewName", model);

Comments

0

Please review your code, you might be getting that error because you are using this code: @{ Html.RenderPartial("ViewName", model); } inside any other @{} clause. Please, read again carefully the error message you get in the browser, you don't need to use the @{} to switch to code because you already are inside, so, just remove @{ and } and you should getting it working properly.

Comments

0

Use @Html.Partial(). It has three overloads, pass the parameters as per your requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.