Convert webform control to Razor syntax

2019-08-08 21:00发布

问题:

i got this control and it works fine, but i need to use it on my mvc3 project. I tryed my way but didnt work. I hope someone can help me. Thats the webform custom control code:

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>

CodeBehind:

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

This custom control inherits from webcontrol. It basically render a table. I tryed do this with Razor but didnt work. Didnt have any errors also.

@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:

You cannot use server side controls in an ASP.NET MVC application. While this was still possible (but not recommended) with the WebForms view engine with Razor this is no longer possible. If you absolutely need to call server side controls try using the WebForms view engine for the particular view in which you need to call the control. But if this control relies on ViewState and PostBacks it won't work.