tags:

views:

196

answers:

5

Ok, this is for me a very tough challenge. We're taking our existing ASP.NET website and converting (redesigning the PL only) to MVC. Our site is very complex. But the hard part is to convert the existing custom controls to MVC equivilant. The custom controls (I am not talking about user controls) are just of course a class currently that inherits System.Web.UI.Control and uses that object throughout. For example, we have some properties at the top of this existing custom class like so:

Dictionary<int, Control> configControls; 
DropDownList kControl; 
CheckBox confirmBox;

These all are variables of type Web controls in classic ASP.NET.

So I figured maybe what I could do (without building entire new custom controls from scratch) is to use the HtmlHelper object. So I tried this:

(include first the using statement that includes System.Web.MVC.Html at the top of my new custom class in our new web project)

private HtmlHelper helper; Dictionary configControls; helper.DropDownList

but this is not working. I guess I can't use this object just like this ?? I figured I can use HtmlHelper in the Dictionary and then make variable types off of helper. but those are just extension methods, not objects

I don't know of an equivalent to something like the generic "Control" we had available to us to inherit from such as in classic ASP.NET. Surely it won't be the same in MVC obviusly (stateless and a completely diff way of doing things) but what can I use in MVC with the same concept sort of?

So I figured maybe what I could do (without building entire new custom controls from scratch) is to use the HtmlHelper object. So I tried this:

(include first the using statement that includes System.Web.MVC.Html at the top of my new custom class in our new web project)

private HtmlHelper helper; 
Dictionary configControls; 
helper.DropDownList

but this is not working. I don't even know if this approach will work in my custom control. And when I try to use my helper variable, I get no extension methods unless it's inside an existing extension method where the signature has an HtmlHelper param passed in. So when I create that private variable just in my custom class outside, I get nothing in intellisense to choose from when doing "helper.". So do I need to define that object like this: ?

private HtmlHelper htmlHelper = new HtmlHelper();

but it's asking for a ViewContext and an IViewDataContainer as params. If I'm building out a custom method that knows nothing yet about its view (it shouldn't need to) because I'm simply creating strings of HMTL in this custom class to be passed to the Extension method to ultimately spit out fields then maybe I can't use HtmlHelper this way in a custom class like this.

So can I use that object in a way instead of "Control"? Maybe I can even in my dictionary variable use type object in place of control ? I don't know and then cast object to type HtmlHelper when I need to use or reference that value from the dictionary? But for now, I figured I can use HtmlHelper object in the Dictionary and then make variable types off of helper. but those are just extension methods, not objects.

I hope I am making any sense here when you read this.

A: 

I would create needed business logic, shared partial view (probably, with quite a lot of well structured javascript lines attached) and seperated controller.

Then i would use this bunch of code through partial request technique.

Arnis L.
tough concept to grasp after reading that. I don't see how that's going to help me in a CUSTOM control where I'm creating methods that form together strings of HTML and controls that ultimately is outputted to a view. I don't really need a partial view in this case..not necessary. I'm talking about a custom class here. Methods that return a string of html that represents and creates an entire form dynamically.
CoffeeAddict
I don't think we're on the same wavelength here
CoffeeAddict
What you are describing then would be a helper. A helper is a static method on a static class, that returns markup based on supplied parameters.
Chad Ruppert
I know that you are talking about custom control. I just forgot to add, that my approach would involve a lot of rewriting existing custom control of yours. :)
Arnis L.
Yea, I don't see a way to do this in MVC, I am going to create a helper to ultimately spit out what my custom class produces (the HTML it strings together). My problem is how in my custom class am I gonna do this when in our existing classic ASP.NET custom class we're using the Control object and casting it / converting it to lets say Dropdown, radio, etc. in the methods in the custom class that is stringing together this HTML for our form. A helper will do nothing mroe than render what my custom class created (the final string) to the View...the helper is not the issue here.
CoffeeAddict
Another bizzare idea would be to attach controller to your custom control class. That would involve a lot of redundant code just for proper communication between UI and your control but might work.
Arnis L.
A: 

Not sure how much this will be of help but, do have a look at this series of blog post

Custom controls everywhere

Also have a look at the Catharsis project Web-Application Framework - Catharsis - Part I - New Solution

The codeplex URL for the same is Catharsis

This project has some good examples of control creating for asp.net mvc.

rajesh pillai
Yea, but that's way overboard for what we need here I think. I don't want to be creating all these custom controls like this. I just want to spit out HTML and create the HTML for our dynamic web form from my custom Control. I hope not to have to create a shitload of other custom controls just to dynamically generate HTML for each type of field I need my custom control to produce and return back as HTML string to my extension method.
CoffeeAddict
and we're not interested in using a 3rd party framework to do this, we want control of our own custom code just using MVC vanilla objects.
CoffeeAddict
No probs. I just pointed to that framework because it is an opensource and has some design ideas with respect to custom controls for asp.net mvc.And the other part is it depends on the project which you are doing which may adopt a particular approach to development (some kind of reusable UI framework), but it depends. Thanks for the comment.
rajesh pillai
Thank you for that.
CoffeeAddict
+2  A: 

I just blogged about this last night, some of this might be helpful for you.

WebForms And MVC In Harmony — Almost…

Basically it discusses some options for emulating "WebControls" using MVC.

Additionally, you can still use WebControls like you could before (granted they may not work if they need things like the ViewState). The problem I've discovered with that is you have a disconnect from the inline render code and the WebControls themselves.

I did write this method last night which let you use WebControls with inline code.

using System.Reflection;
using System.IO;
using System.Web.UI;
using System.Web;
using System.Web.Mvc;

public static class MyExtensionMethods {

//example method - renders a webcontrol to the page
public static void RenderControl(this HtmlHelper helper, Control control) {

    //perform databinding if needed
    MethodInfo bind = control.GetType().GetMethod("DataBind");
    if (bind is System.Reflection.MethodInfo) {
        bind.Invoke(control, null);
    }

    //render the HTML for this control
    StringWriter writer = new StringWriter();
    HtmlTextWriter html = new HtmlTextWriter(writer);
    control.RenderControl(html);

    //write the output
    HttpContext.Current.Response.Write(writer.ToString());

    //and cleanup the writers
    html.Dispose();
    writer.Dispose();
}

}

//then used like...
<% int[] numbers = { 1, 2, 3, 4, 5 }; %>
<% this.Html.RenderControl(new DataGrid() { DataSource = numbers }); %>

Just an interesting concept you might be interested in.

Hugoware
damn thanks. Complicated shiza.
CoffeeAddict
Clever, but that's a hack. :)
Arnis L.
I won't argue - :)
Hugoware
+1  A: 

Short of hacking webforms controls into your MVC application, servercontrols with many methods do not map to MVC.

They are replaced by partials and controllers(or subcontrollers if you like that sort of thing).

If all you want to do is render some HTML based on a few parameters, then a Helper is what you are after. Static Class, static methods. If however, you need to keep state, and do a bunch of stateful stuff, then a partial, JS, and controller(or subcontroller) are really what you are after.

Server Controls that manage their own state really are a thing of the past in MVC.

Remember that MVC is an attempt to use the web the way it was meant to work, particularly if you bring REST into the picture. Webforms is a fudge to make the web work like windows forms.

Chad Ruppert
I think the same - web is stateless after all. But coffeaddict wants this otherwise.
Arnis L.
Yea, I totally understand that but I just want to spit out HTML to my view, I don't see why I need a partial or JavaScript to do this. All I want is to be able to reproduce the custom control we had in a stateless way to pass that HTML back upstream to my Extension method. Then you just keep state using Contexts (ControllerContext, ViewContext, or whatever) in your Controller that are available to you.
CoffeeAddict
Note that I am not saying that you need to use JS or anything else if all you want to do is spit out HTML. That is a helper exactly. Without knowing EXACTLY what your custom control does, this is the most complete answer I can provide.
Chad Ruppert