在MVC使用ASP.NET服务器控件? [英] Using ASP.NET Server Controls in MVC?

查看:166
本文介绍了在MVC使用ASP.NET服务器控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的当前项目我需要增加一个功能,允许用户查看他们的上传PDF格式的缩略图。我发现一个实现这种(基本版本是免费的方便组件,但它足以让我目前的需求)。不管怎么说,控制pretty过时(2010年),因此似乎没有成为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:

该视图的标记:

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

缩略图控制是通过code实例化,字节数组,再presents缩略图传递给控制和控件添加到 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>

鉴于

这是我不能宣布我的剃须刀视图中的缩略图控制,我将如何使用MVC这种控制?我花了几个小时,试图使这种控制MVC友好无济于事,我已经拿出最好是在自己的项目一个.ASPX视图(not.cshtml),并呈现在该视图中的缩略图控制。显然,这是不可取的。

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.

所以,你会怎么去MVC中使用ASPX服务器控件?的理念是一个坏的干脆,不应该实行?

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?

推荐答案

我的工作围绕它在我的一个项目通过重新实现控制作为的HtmlHelper。假设控制不是太复杂那么它应该为你工作了。做到这一点:

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. 导出使用反射控制的源

  2. 按摩源,因此它实际上编译(从反射源通常不会编译直线距离)

  3. 识别控制有什么状态。转换成员属性状态变成自己的新的ViewModel类的成员。

  4. 找到Render方法,并将其转换到使用 ViewContext.Writer
  5. 系统的HtmlHelper
  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

例如:

public class FooControl : Control {
    public String Message { get; set; }

    public override void Render(HtmlTextWriter wtr) {
        wtr.WriteLine("<p>");
        wtr.WriteLine( message );
        wtr.WriteLine("</p>");
    }
}

这变为:

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

这篇关于在MVC使用ASP.NET服务器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆