Levimatt
Levimatt

Reputation: 453

Pass a model from view to a partial view

I have a view that needs to pass data (n times iterated in loop) through the model it's using to other partial view. That partial view displays data for each iteration it's done.

The code of the view is:

@model IEnumerable<Domain.Car>

<table>
@foreach (var item in Model) {
    <tr>
        <td>
            @{Html.RenderPartial("~/Views/Shared/_Details.cshtml", item);
        </td>
    </tr>
}
</table>

The code inside the partial view is the following:

@model Domain.Car

<div>
    Title: @Model.Title
    Description: @Model.Description
</div>
<hr>

But after doing this I can't get any data to be displayed when using the partial view. No errors shown but returns no data.

Could anybody help me with this issue?

Thank you in advance.

Upvotes: 1

Views: 2095

Answers (1)

Renan Ara&#250;jo
Renan Ara&#250;jo

Reputation: 3641

Use Html.Partial instead of RenderPartial:

 @Html.Partial("~/Views/Shared/_Details.cshtml", item);

Html.Partial returns a String, Html.RenderPartial calls Write internally, and returns void.

Take a look at this answer: https://stackoverflow.com/a/5248218/5071902

Upvotes: 2

Related Questions