Using ASP.NET Server Controls in MVC?

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 basic version is free, but it’s enough for my current needs). Anyways, the control is pretty outdated (2010), therefore there doesn’t seem to be MVC support. On the demos they depict usage of the control as such:

The View’s Markup:

<form method="post" runat="server" enctype="multipart/form-data">
       <asp:Panel ID="thumbnailsPanel" runat="server" />
</form>

The thumbnail control is instantiated via code, the byte array which represents the thumbnail is passed to the control and the control is added to thumbnailsPanel

<script runat="server">
protected void DisplayThumbs_Click( object sender, System.EventArgs e )
{
      Thumbnail thumbnail = new Thumbnail();
      thumbnail.SessionKey = sessionID;
      thumbnail.Index = i;
      thumbnailsPanel.Controls.Add( thumbnail );
}
</script>

Given that I can’t declare a Thumbnail control in my razor view, how would I used this control in MVC? I’ve spent a few hours trying to make this control MVC friendly to no avail, the best I’ve come up with is to include a .ASPX view (not.cshtml) in my project and render the Thumbnail control on that view. Obviously this is not desirable.

So how would you go about using a ASPX server controls in MVC? Is the idea a bad one altogether and should not be practised?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x