Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering partial views with Razor in MVC5

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?

like image 438
Mourndark Avatar asked Jun 26 '14 20:06

Mourndark


People also ask

How do I render a partial view in Razor?

For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View. Now for the View -> Home -> Index. cshtml. Here I am rendering a Partial View using 4 types, so the index.

How do I render a partial part of a view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

Which is the way to render partial view using MVC Razor engine?

RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.

How do you render a partial view inside a view in MVC?

Partial function which renders the Partial View. The name of the View and the object of the CustomerModel class are passed to the @Html. Partial function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


3 Answers

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(...);
}
like image 168
Chris Pratt Avatar answered Nov 03 '22 18:11

Chris Pratt


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

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

like image 24
Developer Avatar answered Nov 03 '22 19:11

Developer


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.

like image 41
Believe2014 Avatar answered Nov 03 '22 20:11

Believe2014