简体   繁体   English

将其他参数传递给ASP.Net Core 2.0 Razor中的Html.Partial

[英]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. 我在模型中遍历一个列表,并将数据发送到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. 我想在同一页面上的两个不同位置使用此部分,但是我需要区分某些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: EmployeeTableRow部分:

<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. 实际的Model数据包含大量的信息,如果可能的话,我宁愿不必使用new {}进行具体定义。

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. 您可以为此使用ViewBag,但应尽量减少使用它,以保持代码干净。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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