0

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!

1 Answer 1

2

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">
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip! I agree about keeping things like this to a minimum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.