简体   繁体   中英

Passing additional parameters to Html.Partial in ASP.Net Core 2.0 Razor

I'm looping through a list in my Model and sending the data to an Html.Partial.

I'd like to know if I can pass an additional string value that isn't in the indexed model. I want to use this partial in two separate spots on the same page but I need to differentiate the data target for some Javascript.

For example, the loop in my View currently looks like this:

@for (var i = 0; i < Model.Team.Count(); i++)
{
    @Html.Partial("EmployeeTableRow", Model.Team[i])
}

and the EmployeeTableRow partial:

<tr data-target="[additional string variable]">
    <td>@Model.Fname</td>
    <td>@Model.Lname</td>
</tr>

The actual Model data contains a great deal more info that I'd rather not have to specifically define using new {} if possible.

Any help or links to duplicate questions would be appreciated!

you can use ViewBag for this but you should try to minimize the use of it to keep your code clean.

    @Html.Partial("EmployeeTableRow", Model.Team[i])
    ViewBag.AdditionalString = "something"
------------------------------------------------------
    <tr data-target="@ViewBag.AdditionalString">

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