将webform控件转换为Razor语法

时间:2012-05-28 19:28:44

标签: asp.net asp.net-mvc-3 razor

我有这个控件,它工作正常,但我需要在我的mvc3项目上使用它。我尝试了自己的方式,但没有工作。我希望有一个人可以帮助我。这是webform自定义控件代码:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BoletoCS.aspx.cs" Inherits="BoletoCS" %>
<%@ Register Assembly="Impactro.Cobranca" Namespace="Impactro.WebControls" TagPrefix="cob" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Boleto</title>
    <style type="text/css">
    .BolCell { font-size: 7pt; font-family: Verdana; }
        .BolField { font-weight: bold; font-size: 12px; font-family: arial; }
</style>
</head>
<body>
    <form id="form1" runat="server">
         <cob:BoletoWeb id="bltPag" runat="server" CssCell="BolCell" CssField="BolField" ></cob:BoletoWeb>
     </form>
</body>
</html>

代码隐藏:

using System;
using Impactro.Cobranca;

public partial class BoletoCS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var cedente = new CedenteInfo
        {
            Cedente = "CURRICULO AUT ASS E CONS EM RH",
            Banco = "341",
            Agencia = "6157",
            Conta = "30901-1",
            Carteira = "175",
            CNPJ = "14.765.492/0001-10"
        };

        var sacado = new SacadoInfo { Sacado = "RODRIGO MANGUINHO" };

        var boleto = new BoletoInfo
        {
            NossoNumero = "44",
            ValorDocumento = 99,
            DataDocumento = DateTime.Now,
            DataVencimento = DateTime.Now.AddMonths(1),
            LocalPagamento = "PAGÁVEL EM QUALQUER BANCO ATÉ O VENCIMENTO.",
            Especie = Especies.RC,
            Instrucoes = "NÃO ACEITAR PAGAMENTO APÓS O VENCIMENTO."
        };

        bltPag.MakeBoleto(cedente, sacado, boleto);
    }
}

此自定义控件继承自webcontrol。它基本上呈现一个表。 我尝试用Razor做这个但是没有用。也没有任何错误。

@using Impactro.WebControls
@using Impactro.Cobranca

@{
    var ci = new CedenteInfo
    {
        Cedente = "CURRICULO AUT ASS E CONS EM RH",
        Banco = "341",
        Agencia = "6157",
        Conta = "30901-1",
        Carteira = "175",
        CNPJ = "14.765.492/0001-10"
    };

    var si = new SacadoInfo { Sacado = "RODRIGO MANGUINHO" };

    var bi = new BoletoInfo
    {
        NossoNumero = "44",
        ValorDocumento = 99,
        DataDocumento = DateTime.Now,
        DataVencimento = DateTime.Now.AddMonths(1),
        LocalPagamento = "PAGÁVEL EM QUALQUER BANCO ATÉ O VENCIMENTO.",
        Especie = Especies.RC,
        Instrucoes = "NÃO ACEITAR PAGAMENTO APÓS O VENCIMENTO."
    };

    var bw = new BoletoWeb
    {
        CssCell = "",
        CssField = "",
        ImagePath = Url.Content("~/images/bank-ticket")
    };

    bw.MakeBoleto(ci, si, bi);
}

1 个答案:

答案 0 :(得分:0)

您无法在ASP.NET MVC应用程序中使用服务器端控件。虽然使用Razor的WebForms视图引擎仍然可以(但不推荐),但这已经不可能了。如果您绝对需要调用服务器端控件,请尝试使用WebForms视图引擎来查看需要调用控件的特定视图。但是,如果此控件依赖于ViewState和PostBacks,它将无法工作。