How do I send html from one view to another view?

Asked

Viewed 138 times

1

I have a view (View1.cshtml) with filters, a whole layout and a graph within a div that is generated according to the filters.

I created another view (View2.cshtml) in white and I would like to pass to her only this div with the generated graph.

Assuming I have this link in View1:

<a href="View2">Enviar HTML</a>

And supposing my controller is that way:

    public ActionResult View1()
    {
        negocio = new View1Negocio();

        View1DTO dto = new View1DTO ();

        dto.LstExercicios = negocio.ObterExercicios();

        return View(dto);
    }

    public ActionResult View2()
    {
        negocio = new View1Negocio();

        View1DTO dto = new View1DTO ();

        dto.LstExercicios = negocio.ObterExercicios();

        return new ViewAsPdf("View2", dto);
    }

Is there any way to do this? What would I have to do in this code to send this html?

1 answer

1

In fact from what I understand, you want to generate the graph and the information all within a PDF.

I’ve never done this, but there is a Nuget package that accesses a website and converts it to PDF:

https://www.nuget.org/packages/Pechkin

The project’s website is:

https://github.com/gmanny/Pechkin

Except that Pechkin does not work with HTML internally. What it does is act as a Crawler that downloads the content of an address and converts it to PDF.

If this resolves, change the following code in your View:

public FileResult View2()
{
    // configuração global
    GlobalConfig gc = new GlobalConfig();

    // margens, nome do documento, tamanho do papel...
    gc.SetMargins(new Margins(300, 100, 150, 100))
      .SetDocumentTitle("Test document")
      .SetPaperSize(PaperKind.Letter);
    //... etc

    // conversor
    IPechkin pechkin = new SynchronizedPechkin(gc);

    // callbacks
    pechkin.Begin += OnBegin;
    pechkin.Error += OnError;
    pechkin.Warning += OnWarning;
    pechkin.PhaseChanged += OnPhase;
    pechkin.ProgressChanged += OnProgress;
    pechkin.Finished += OnFinished;

    // configuração do objeto
    ObjectConfig oc = new ObjectConfig();

    // codificação, imagens e URL
    oc.SetCreateExternalLinks(false)
      .SetFallbackEncoding(Encoding.ASCII)
      .SetLoadImages(false)
      .SetPageUri("http://enderecodoseusite.com/ControllerDoPDF/View1");
    //... etc

    // gera o PDF
    byte[] pdfBuf = pechkin.Convert(oc);

    return File(pdfBuf, System.Net.Mime.MediaTypeNames.Application.Octet,
                "SeuArquivo.pdf");
}

I think you’ll need to generate a graph parameterizing the Action to be able to work, not fill in the screen as I imagine being done today.

  • Thank you, I think Pechkin does the same as I am using: Rotary: https://github.com/webgio/Rotativa perhaps yours is more complete. It’s a lot of stops for my chart generation, so I didn’t want to go through the action. But if I can not pass the html I think I will not have option.

  • 1

    Do not use Rotary. It causes problems in certain environments. I already reported the error to the developer almost a year ago and he said he would not correct.

  • Good to know. I’ll try to replace Pechkin then.

  • Hi João Paulo, I read your comment stating that there are many parameters for graph generation. In this case would not it be interesting to work with the View being the type of your Model (example Graficoviewmodel)? I ask this because that way, when giving a post in View1 your parameters would all be sent to an Action that receives a parameter of type Graficoviewmodel where you can use them and build your View2. I believe this could help you or be an interesting alternative.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.