-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invoke MVC ViewComponent with generic type parameter #9828
Comments
Razor component is actually a Blazor component. You meant ViewComponent, right ? Language is the source of misunderstandings... |
Yes you are right, my bad... I will edit it. |
Hi @ajruckman - we don't have support for using generics in View Components. |
Hi @rynowak, I was pretty sure that was the case so that's why I thought I should use the feature request template. Do you think there is any possibility of having generic View Components in the future? If not I will understand but I think they could be very powerful. Thank you! |
It's really unlikely that we'll add this as a feature to view components since we consider that feature area pretty much done. What would help us understand the need for this more would be an example of seeing where Most of the infrastructure we provide for output (views/action results) uses the runtime type of the object. So having a base class (or using |
I have pretty much solved all of the problems I had when I opened this issue with I define classes that act as schema for things I want to display on a page. Each property has multiple attributes that define how it should be displayed. Here is a sample: public class Change
{
[Contexts(Context.List, Context.Form, Context.Info)]
[HiddenIn(Context.Form)]
[ReadOnly]
public int ID { get; set; }
[Contexts(Context.List, Context.Form, Context.Info)]
[InputType(InputTypes.Select)]
public string Type { get; set; } = null!;
[Contexts(Context.Info)]
[DisplayName("Type Description")]
public string TypeDescription { get; set; } = null!;
} One use case I have for generic components is that I want to pass a With generics, I wouldn't have to use reflection to get the class of elements in the List, and then create a new instance of that class. I have other classes like This is probably way overcomplicated so I understand that it's likely not worth it to add generics support for components if it would require a lot of work to do so, but... |
Thanks for all the details, @ajruckman. |
Is your feature request related to a problem? Please describe.
I would like to be able to invoke a Razor Pages ViewComponent with a generic type parameter. For example, I want to be able to pass any List to a component, like this:
and to somehow call the component in a .cshtml file:
but I don't know where the type specifier would go in the .cshtml file.
Describe alternatives you've considered
Using an "object" type instead of a generic list parameter. But this isn't very clean.
below is kindof a different issue but maybe it's relevant; if this worked, I might not need generic parameters
I tried to ensure that the 'object' passed is any type of List with a function like this:
but this function doesn't work; it seems like the 'object' is a:
rather than a
System.Collections.GenericList
; maybe Razor is modifying the 'object' or something, or maybe I misunderstand how generics work.The text was updated successfully, but these errors were encountered: