Search code examples
model-view-controllergenericsviewpartial-viewspartial

Generic Partial View


I just want to ask if it is possible to create a partial view that accepts a generic type of parameter?

Basically, I'm creating an add form which can be used to add different types of objects (ie Products, Employee). Currently, I can create this through a PartialView which accepts my ProductsModel to render the Add Products form. However, I want to use this same form for my Add Employee.

What I have in mind is to attach the type to render via a query string and check on the code which one of these to render. I tried it but it doesn't seem to succeed.

Can someone point me to a solution? Thanks a lot! Cheers!


Solution

  • This is just a suggestion for an alternate approach that MAY give you the functionality you need.

    Declare a ViewModel object called e.g. "WidgetModel" with "n" properties - call them Property1, Property2, etc

    Map the objects that you want to bind to the view, to this common object - you can use Automapper to achieve this very easily.

    Now in the view you will be binding to the properties of the WidgetModel object and there will be no problem with types.

    Personally though, I think I stick with separate partials!

    Hope this gives you some ideas.