cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
https://www.cofoundry.org
MIT License
819 stars 144 forks source link

Improve ViewComponent ModelBinding Support #125

Open HeyJoel opened 7 years ago

HeyJoel commented 7 years ago

ViewComponents are a replacement for child actions which we've used previously to encapsulate component logic in a page template that can't easily be done inline, e.g. listing of blog posts, or a contact form.

Unfortunately ViewComponents don't support model binding, and often we need to react to parameters in the query or form post of the request such as with a contact form submission or for paging a query. For now in the sample projects we just inspect the query or form data in the request manually, but it would be better if we could work out a way to make this easier/simpler. Ideally we'd be able to inject in a model binding service that will take care of the logic for us.

JornWildt commented 3 years ago

It would be nice to see some work on this :-) I'm implementing a booking system with a "request booking" form which is much like a contact form - just more complex and ends up creating a "Booking" custom entity.

Maybe I am better off writing a custom controller and skip all the Cofoundry stuff for this sort of thing?

HeyJoel commented 3 years ago

This is a feature of ASP.NET that was removed in .NET Core (child actions used to support this and that's how we used to handle it). I was hoping that a more modular model binder would be introduced in later versions of .NET Core, but I haven't looked into it recently. I expect most people post forms over AJAX/API Endpoint these days, but yes you can also write your own controller if you don't need CMS templating.

JornWildt commented 3 years ago

Your comment about AJAX/API got me looking into Vue.js (I'm still a bit old school and like server side coding) - and I think you are spot on. View Components are not a good fit for "normal" forms handling - with VC you can end up with many forms on one page that post back to the same page ... and how do you then know which of the VC's going to handle that POST? No - I think AJAX forms are way better for this kind of "componentization" where each VC can call back to it's own API endpoint.