Using ASP.NET Server Controls in MVC?

前端 未结 1 1033
无人及你
无人及你 2021-01-16 06:56

On my current project I need to add a functionality that allows the user to view a thumbnail of their uploaded PDF. I\'ve found a handy component that achieves this (the ba

相关标签:
1条回答
  • 2021-01-16 07:15

    I worked around it in a project of mine by reimplementing the control as a HtmlHelper. Assuming the control isn't too complicated then it should work for you too. Do this:

    1. Dump the Control's source using Reflector
    2. Massage the source so it actually compiles (as source from Reflector doesn't usually compile straight away)
    3. Identify what state the control has. Convert the state from member properties into members of its own new ViewModel class.
    4. Find the Render method and convert it to a HtmlHelper that uses ViewContext.Writer

    For example:

    public class FooControl : Control {
        public String Message { get; set; }
    
        public override void Render(HtmlTextWriter wtr) {
            wtr.WriteLine("<p>");
            wtr.WriteLine( message );
            wtr.WriteLine("</p>");
        }
    }
    

    Becomes this:

    public class FooViewModel {
        public String Message { get; set; }
    }
    
    // This method should exist in a static Extensions class for HtmlHelper
    public static void Foo(this HtmlHelper html, FooViewModel model) {
        HtmlTextWriter wtr = html.ViewContext.Writer;
        wtr.WriteLine("<p>");
        wtr.WriteLine( model.Message );
        wtr.WriteLine("</p>");
    }
    
    0 讨论(0)
提交回复
热议问题