How would you do this using the new <partial /> tags:
@foreach (var scope in Model.ResourceScopes)
{
@Html.Partial("_ScopeListItem", scope)
}
The _ScopeListItem partial takes a ScopeViewModel of course, but I need to pass the specific one in the for loop, not just any "ScopeViewModel". If I am understanding the post correctly, are we now forced to do this using [ViewData]?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Nevermind. My apologies, missed the @ on the scopes variable. Duh.
@regul4rj0hn I'll add a "Migrate from HTML Helpers" section to the doc, as I'm sure others are curious too. In your case, you'll want to use the following:
@foreach (var scope in Model.ResourceScopes)
{
<partial name="_ScopeListItem" model="scope" />
}
Also note that you gain the benefit of asynchronous loading of the partial view when using the Partial Tag Helper.
Yep, that might help people. It would actually look like this to get it to compile:
@foreach (var scope in Model.ResourceScopes)
{
<partial name="_ScopeListItem" model="@scope" />
}
Otherwise, it looks for "scope" inside the current @model tagged for the view, instead of passing in the foreach variable.
Good to know about the async load, thanks!
@regul4rj0hn It compiles and works for me without the @ symbol before scope. If you run dotnet --version, what's the version number returned? Also, which version of VS are you using?
> dotnet --version
> 2.1.301
VS Professional v17.7.4
https://imgur.com/ufby9ow
Maybe something to do with ResourceScopes being IEnumerable<Scope>? what are you iterating over?
@regul4rj0hn I see the issue. You're assigning scope to the for attribute instead of the model attribute.
Whoops! You are absolutely correct, that happens when you take a shortcut, and do a find and replace on the @Html.Partial tag across your project to "migrate to 2.1 faster". Thanks for spotting the oversight. I should point out that the other (incorrect) form of for="@scope" also works just fine, though.
Thanks a lot for your help, Scott!