views:

300

answers:

3

I've started using MVC reccently, and one thing that occurs to me is whether its possible for the concept of a Control to exist in MVC?

From what I see the framework allows the application to be nicely factored into Models, Views and Controllers, but I can't think of a nice way to take a "vertical slice" of that application and reuse it in another application.

What I mean by that is it strikes me that any UI components I build with MVC maybe not very amenable to reuse, in the same way you can reuse a Contol in ASP.NET WebForms. Ok, there are HTML Helpers but I am thinking of something more modular. Something more like the control model in WPF.

Does this dichotomy go to the heart of MVC vs WebForms, or can reusable UI components work in an MVC world?

+2  A: 

You can still use ascx files and other features in ASP.NET Web Forms. The only missing piece in MVC is the postback model and view state oriented state management. There is no <form runat="server"> anymore and no automatically generated hidden fields. Except these, all other features can be used in ASP.NET MVC. You can still write controls that post data using a REST based mechanism and use them in your views.

So, yes, as long as your controls don't rely on a server side form for postback, you can use them exactly the same way you'd use in ASP.NET Web Forms.

Mehrdad Afshari
i meant this question more conceptually... not "can i use controls in MVC"... more "is there a way to share UI/other components accross many MVC projects"
Schneider
+1  A: 

I guess its harder in MVC to completely encapsulate rendering and post back handling in a single control you just drop on a page. It some ways this is because ASP.NET Webforms is very abstracted from HTTP semantics and that abstraction is a framework where its possible to create reusable user controls.

ASP.NET MVC doesn't have the abstraction of webforms so you can have a control the posts to three different controllers. While it may feel like your losing easy to use controls when you move to ASP.NET MVC, I think you have a better framework for separating and reusing domain logic.

In ASP.NET MVC you have partial views which can be reused. Rob Conery has a good post on this: ASP.NET MVC: Using UserControls Usefully

tarn
Very interesting link. I wonder if using bits of the WebForms library (UserControl in this case) in your MVC app is in anyway detrimental. Doesn't seem so "pure"?
Schneider
I wouldn't bother, I just don't think the webform controls offer that much value in MVC. I go with the partials.
tarn
+2  A: 

I was looking for the same thing - for reusable widgets with their own data paths and found this on Steve Sanderson's Blog:

http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/

From the article:

"You’ve heard of partial views, so how about partial requests? Within any MVC request, you can set up a collection of internal partial requests, each of which can set up its own internal partial requests and so on. Each partial request renders a plain old action method in any of your plain regular controllers, and each can produce an independent widget."

This article respectfully offers a alternative to The MVC Contrib Group's Sub Controller strategy (http://www.mvccontrib.org/) which is also a solution for what you are looking for.

Adam Tolley