how to get the html code of the rendered div in the MVC Controller?

Dondon510 221 Reputation points
2022-09-27T23:04:15.617+00:00

Hi All,

I need to do convert a rendered div of a html page to pdf on-the-fly, how to get the html code of the rendered div in the MVC Controller?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,194 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2022-09-28T16:20:11+00:00

    the controller only builds the model. the view renders the html. to get the html as a string in the controller, it must directly call a view render directly. see this article:

    https://weblog.west-wind.com/posts/2022/Jun/21/Back-to-Basics-Rendering-Razor-Views-to-String-in-ASPNET-Core

    note: the more common approach is to use javascript to render the pdf. see:

    https://mozilla.github.io/pdf.js/examples/

    0 comments No comments

  2. Laxmikant 216 Reputation points
    2022-09-28T19:07:43.787+00:00

    you can use model binding

    in view

    @model MVC.Models.Address  
      
    <div>@Html.Raw(Model.AddressDetails)</div>  
    

    in controller

       [HttpPost]  
        public ActionResult Index(Address address)  
        {  
            string hrmlstring = address.AddressDetails  
            return View(person);  
        }  
    
    0 comments No comments