cofoundry icon indicating copy to clipboard operation
cofoundry copied to clipboard

Improve ViewComponent ModelBinding Support

Open HeyJoel opened this issue 7 years ago • 3 comments

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.

HeyJoel avatar Jun 28 '17 14:06 HeyJoel

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?

JornWildt avatar Mar 14 '21 13:03 JornWildt

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.

HeyJoel avatar Mar 14 '21 20:03 HeyJoel

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.

JornWildt avatar Mar 20 '21 11:03 JornWildt