views:

175

answers:

2

I only know a small amount about .NET MVC and haven't used it barely at all so far but I was wondering how would you create re-useable controls that can be spread across different applications?

We have various controls that have been created in external libraries and we reference the assembly if we want to use them. An example would be a date range selector for reports, the user can select two dates which they then post back the page and the controls changed date event is fired and the report is updated.

How would you accomplish this in MVC? I know you can still use the control like you would do normally but this doesn't seem like the way MVC should be used. I thought that normally you would post the form data to a new view, validate the form data and then do whatever is required next. However when this is all encapsulated inside a control how can you possibly split it out over multiple views?

Some points I'm hoping to get out of this question are:

  • How do you get around this?
  • What sort of work flow do your controls have?
  • Do your forms actually post to other pages or do they post back to the same page?

** Update:**

I have found this blog entry from Rob Conery but it doesn't deal with controls it deals with usercontrols. Even with the below method of rendering a control how would you use it to render a control which dealt with a form?

http://blog.wekeroad.com/2008/01/07/aspnet-mvc-using-usercontrols-usefully/

A: 

You can use pure javascript controls which can then "post back" via ajax. On the server side you have a controller + action which handles the date range changes.

A calendar example

Todd Smith
+2  A: 

For reuse of view elements, user controls and extensions of HtmlHelper should suit your needs. I have created a project that extends view pages to generate view elements using a fluent interface. Read about it here.

However, these are all view-only approaches. For reuse of UI element that include server side processing MvcContrib has developed the concept of sub-controllers. Read about it here. Here's another blog post that describes an alternative approach that claims to be less complex using "partial requests." I have not ventured into this territory myself yet.

Tim Scott
Looks interesting I will certainly have a look into it, thanks Tim.
John_