简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM