aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Pass generic type/list to ViewComponent's Invoke method #8699

Closed fabercs closed 5 years ago

fabercs commented 5 years ago

Description of the problem:

I want to make a generic list component in my application. List components are called as follows; @await Component.InvokeAsync("ListComponent", new { list = Model.List, columnsList= "Subject,Description" })

The list in Model.List contains a List<T> where T is a type derived from a base IEntity. Here the components Invoke

public IViewComponentResult Invoke(List<Entity> list // <== strong type , string columnsList)
{
    // omitted for brevity
    return View("Default", list);
}

this works fine, however when change to this list starts coming null.

public IViewComponentResult Invoke(List<IEntity> list // <== generic type , string columnsList)
{
    // omitted for brevity
    return View("Default", list);
}

Is there a way to make this work with generic types?

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All: Asp.Net Core 2.1

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @fabercs. @dougbu, can you please look into this? Thanks!

fabercs commented 5 years ago

@mkArtakMSFT , @dougbu I have checked my code with a refreshed eye and Invoke parameter name and anonymous type parameter name in caller were mismatching. No worries.