Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Following from the previous question I've started to wonder - is it possible to implement "Components" in ASP.NET MVC (latest version)? And should you?

Let's clarify what I mean with a "component". With that I mean a "control" (aka "widget"), similar to those that ASP.NET webforms is built upon. A gridview might be a good example. In webforms I can place on my form a datasource component (one line of code), a gridview component (another line of code) and bind them together (specify an attribute on the gridview). In the codebehind file I fill the datasource with data (a few lines of DB-querying code), and I'm all set.

At this point the gridview is a fully functional standalone component. I can open the form, and I'll see all the data. I can sort it by clicking on the column headers; it is split into several pages; I can drag the column headers around and rearrange columns; I can turn on "grouping" mode; etc. And I don't need to write another line of code for any of it. The gridview, as a component, already has all the code tucked away in its classes and assemblies. I just place it on the form, initialize it, and it Just Works. At some times (like sorting or navigation to a different page) it will also perform ajax callbacks to the server, but those too will be handled internally, with my code having no knowledge at all about it. And then there are also events that I can attach if I want to get notified when something happens.

In MVC I cannot see a way of doing this cleanly. Sure, there are the partial views, but those only handle half of the problem - they render the initial HTML. Some more can be achieved with client-side Javascript (like column re-arranging), but when the grid needs to do an ajax callback (say, to fetch the next page of data), my code will have to get involved and process that request. At best I guess I can provide some helper methods to process it, but I'll have to write the code that calls them, and also provide a controller method with signature matching the arguments of that callback.

I guess that I could make some hacks with global events or special routes or something, but that just seems... hackish. Unelegant. Perhaps this is not the MVC way? Although I've completed one project in it, I'm still far from being an MVC expert. But then what is? In the intranet application that we're building there are dozens upon dozens of such grids. Naturally I want them all to have a unified look & behavior, and I don't want to repeat the same code all over the place.

So what's the "MVC" approach to this problem?

share|improve this question
5  
Telerik have MVC controls telerik.com/products/aspnet-mvc.aspx so I guess the answer is yes –  jmo21 Mar 19 '12 at 15:58
    
@Ozz - interesting. I'm checking out the example code, and it looks fair enough. But I don't understand how/where the callbacks are processed. Have you any information on how they've handled this problem? –  Vilx- Mar 19 '12 at 16:05
1  
yep, call backs simply go to Controller actions. So the Grid view has Insert/Update/Delete of rows... each one is mapped to an action, they can be via Ajax, all very easy. –  jmo21 Mar 19 '12 at 16:08
    
@Ozz - what about sorting/paging? Like in the very first example. Those also generate callbacks, but I don't see where they're processed. –  Vilx- Mar 19 '12 at 16:11
    
Just the same as the others, they are processed by a call to an action method with associated params –  jmo21 Mar 19 '12 at 16:39

3 Answers 3

up vote 1 down vote accepted

Yes it is possible to create reusable widgets that play nicely with ASP.NET MVC. As an example, Telerik has two control suites that are "targeted" at the ASP.NET MVC developer. The first is the Telerik Extensions for MVC. These are implemented as HTML Helpers that spit out the appropriate markup (html and jQuery). The second is the Kendo framework which is a pure jQuery based framework that happens to work well with MVC (but also plays nicely with other web frameworks out there).

Both of these are pretty good examples of how to create re-usable widgets that can be dropped in with a few lines of code. The beauty of these frameworks lie in their exstensibility both in terms of functionality as well as look and feel.

share|improve this answer
    
jquery is not markup –  Raynos Mar 19 '12 at 17:04
    
frontend code? Is that better? I'd prefer not to debate semantics. –  Mike Brown Mar 19 '12 at 18:06
    
html would be more semantic. –  Raynos Mar 19 '12 at 18:10
    
LOL Touche mon frer. –  Mike Brown Mar 19 '12 at 18:57

Of course you can, you just have to reimplement all the things webforms does out of the box to make it work but in an efficient manner.

There's no reason you can't have a partial template which renders a "widget" and also progressively enhances that widget with javascript and ajax functionality.

You will have to write some lines of code on the "backend" to support the ajax. Whether thats hundreds of lines of HTTP handling code or 2 lines depends on how much abstraction you push into a framework layer.

share|improve this answer

MVC approach to this problem would be to build a jquery UI plugin and have that render your UI. You could also provide some classes to help feed it data. I don't think there is the same drag-n-drop deployment requirement in MVC for good reason.

share|improve this answer
    
I don't mean drag-n-drop development like in webforms designers, but I do like the concept of "reusable components", which can give me a complex behavior (like a gridview) with only a few lines of initialization code. –  Vilx- Mar 19 '12 at 16:04
    
Yeah, then a jquery plugin style approach is probably your huckaberry. –  Wyatt Barnett Mar 19 '12 at 16:12
    
jQuery plugin is the wrong solution –  Raynos Mar 19 '12 at 16:16
    
@Raynos: Because... –  Robert Harvey Mar 19 '12 at 16:18
    
@RobertHarvey see detailed answer –  Raynos Mar 19 '12 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.