exporting HTML saved in sql server to word in asp.net MVC application

Customer Support 21 Reputation points
2021-12-01T02:51:41.707+00:00

I have a web application developed using ASP.NET MVC, C#, entity framework and SQL Server. I am using a rich editor to save HTML in SQL Server. I am able to render the saved html to webpage correctly using HTML.Raw

<p> @azzedinehtmlsql .Raw(item.TitleContent)</p>

But when I try to export this HTML retrieved using entity framework in my controller class to word document, the HTML formatting is getting messed up. Is there any HTML.Raw equivalent which I can use in my controller class?

Thanks.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,170 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,938 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,061 Reputation points
    2021-12-01T06:49:12.493+00:00

    Hi @Customer Support ,

    the HTML formatting is getting messed up.

    How do you now? I tested @azzedinehtmlsql .Raw(message). And then I try to export html to the word and I success. What's your problem?

    <div id="Grid">  
        @Html.Raw(message)  
    </div>  
      
    @using (Html.BeginForm("Export", "Home", FormMethod.Post))  
    {  
        <input type="hidden" name="GridHtml" />  
        <input type="submit" id="btnSubmit" value="Export" />  
      
    }  
      
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
    <script type="text/javascript">  
            $(function () {  
                $("#btnSubmit").click(function () {  
                    $("input[name='GridHtml']").val($("#Grid").text());  
                });  
            });  
    </script>  
      
     [HttpPost]  
            [ValidateInput(false)]  
            public EmptyResult Export(string GridHtml)  
            {  
                Response.Clear();  
                Response.Buffer = true;  
                Response.AddHeader("content-disposition", "attachment;filename=Grid.doc");  
                Response.Charset = "";  
                Response.ContentType = "application/vnd.ms-word";  
                Response.Output.Write(GridHtml);  
                Response.Flush();  
                Response.End();  
                return new EmptyResult();  
            }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful