Convert ASP.NET Classic User Control to MVC

Well, this is a very difficult task for me. We take our existing ASP.NET website and convert (reverse engineer only PL) to MVC. Our site is very complex. But the tricky part is converting existing user controls to MVC equivilant. Custom controls (I'm not talking about custom controls) is, of course, a class that inherits from System.Web.UI.Control and uses this object everywhere. For example, we have some properties at the top of this existing custom class, for example:

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

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

So, I thought, maybe what I could do (without creating whole new user controls from scratch) using the HtmlHelper object. So I tried this:

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

private helper HtmlHelper; Dictionary configControls; helper.DropDownList

but it does not work. Guess I can't use this object this way? I decided that I could use HtmlHelper in the dictionary, and then make variable types outside the helper. but these are just extension methods, not objects

, - "", , , ASP.NET. , MVC obviusly ( ), MVC ?

, , , ( ) HtmlHelper. :

( using, System.Web.MVC.Html, -)

private HtmlHelper helper; 
Dictionary configControls; 
helper.DropDownList

. , . , , , HtmlHelper, . , , intellisense , "". :?

private HtmlHelper htmlHelper = new HtmlHelper();

ViewContext IViewDataContainer . , ( ), HMTL , Extension, , , HtmlHelper , .

, ""? , ? , HtmlHelper, ? , HtmlHelper , . , .

, , .

+3
4

, .

WebForms MVC - ...

"WebControls" MVC.

, WebControls, ( , , ViewState). , , WebControl.

, WebControls .

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 }); %>

, .

+4

- MVC, servercontrols MVC.

( , ).

, , - HTML , - , . , . , , , , JS ( ) , .

, , MVC.

, MVC , , REST . Webforms - , - Windows.

+2

-, (, javascript) .

.

0
source

Not sure how much this will help, but check out this blog post series

Custom controls everywhere

Also see the Catharsis Project Web Application Framework - Catharsis - Part I - New Solution

Code url for it Catharsis

There are some good examples of creating control for asp.net mvc in this project.

0
source

Source: https://habr.com/ru/post/1710765/


All Articles