asp.net mvc rasor #raw does not accept <br> - asp.net-mvc

I have a solution that uses asp.net web api to convert data to pdf.
I am using #Raw to display the data in a html formatted manner. But the problem is that if the json string contains the html <br> it throws an error. Once I change it manually to XHTML <br/> it works fine.
Why can't #Raw handle html breaks ? Is there a better way to handle html tags?
Description
#Raw(#Model.Description)

For security reasons, Raw method is not recommended for using - it does not encode input string. The better solution, especially when you already use Web API, would be to just return the non-formatted data, and do the html formatting on the client-side, maybe using some templates library.

Related

How to convert the data into html reading from data base

I am developing an application in which I have used the editor so when the user submit the data the data was send to the sql server in HTML format.
<p><strong><em>fghfghfghfghfghfghfgdfxvbc</em></strong></p> <ol> <li>.
it will not converted in to html tags.
I'm using Asp.net Mvc.
The code is as follow:
When adding raw HTML to a page in ASP.NET MVC, you need to use
#Html.Raw("your html string here")

Getting HTML(Readable) Format from RadRichTextBox using MVC

I am building an application on MVC, where we had same application in MVVM silverlight, we had no problem in using PDF's an RadRichTextBox formats, so in MVC we are using same database same as in MVVM, and here I am having some problem as below
in sql server database we have already stored a radrichtextbox formatted data, and using MVC I am trying to get that data and put it back to PDF(which is working fine in MVVM), and I am able to put it on PDF without any problem, but the format which get it on PDF is not in proper order or its not readable, below I am providing example of format what I have on sql server and what format I am expecting it from there
My question
Is there any way we could use that radrichtextbox format to convert it into HTML format so that I can use it on PDF
since I am using MVC is there any dll, so that i can implement to get right format
NOTE
I already searched in stack forum with no help, and I even contacted Telerik people regarding this all they were able to help me out is for silverlight
and This link from telerik rich-editor will help me to get it into right format which I need but finding hard time to implement
So if there any one could able to help me or even guide me in this would be much helpful for me, Thanks
UNFORMATTED DATA WHICH I GET FROM SQL SERVER
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Untitled</title><style type=\"text/css\">\r\n.s_D8D99854 { font-family: 'Verdana';font-style: Normal; } \r\n</style></head><body><p ><span class=\"s_D8D99854\"> hello</span></p></body></html>
AND THIS IS THE FORMAT I SHOULD GET FROM ABOVE
I believe that you can easily strip the content by yourself using the server-side String.Replace method by converting all \" instances to "
If the PDF convertor does not understand the meta tags, the HTML, HEAD and BODY tags then strip them too using regular expressions.

Html.Raw not decoding

Just a query, I have used #Html.Raw(Item.sometext) before and it decodes the html tags correctly, I'm getting some data from remore source which is in json format, but when displayed on the page I found Html.raw did not decodes html tags.
To fix the problem I used:
#Html.Raw(HttpUtility.HtmlDecode(Item.sometext))
So my question is, can anyone please tell me why that could be the case, as I'm curious as to the reason. Im using mvc4 and asp.net 4.5
Thanks
George
Here is my answer in an attempt to explain better what I mean (in the comments).
Your JSON is formatted for example (which you have supplied) like so:
<p><b>Location. <\/b> <br \/>...
However, this is not valid HTML. Notice the escape characters used for the slashes '/'. So if you pass this value to Html.Raw it will (should) output it, but it's not valid HTML so will unlikely display correctly (if it display anything at all).
This escape character issue can be fixed using Html.Decode which will effectively return the following:
<p><b>Location. </b> <br />...
This is valid HTML, and can therefore be passed to Html.Raw without any problems
NOTE: Html.Raw does not do any encoding/decoding, in fact it explicitly instructs that the supplied value should not be encoded as it is already raw HTML. This is confirmed here:
Use the Raw method when the specified text represents an actual HTML
fragment that should not be encoded and that you want to render as
markup to the HTTP response.

Display file (PDF/TXT) on form brought from Database in MVC ASP.NET

I want to display file on my form using MVC.
I am bringing a Byte[] array data from the Database, and using FileContentResult I am converting it into a file.I want to now display this file on my page for viewing . How can it be acheived. What code to write in my View for the same.
Assuming you're using Razor, rendering a text file can be done as simple as:
<div>
#(new System.IO.StreamReader("myFile.txt")).ReadToEnd()
</div>
For PDF files, you'll have to find a third-party component to convert to HTML.
You probably don't want to use FileContentResult, that is something generally used for providing the raw file.
In theory though there is nothing different in using any other url
<img src="#Html.ActionLink("View","Image",{id = Model.key})" />
Or you can provide that link in a pdf reference, or as a stylesheet etc.

How Decode HTML in MVC with RAZOR

I'm using MVC 3 and Razor as View Engine, for my understanding HTML tags are decoding by default to prevent XSS attacks or similar. (I'm also using MS XSS 4.2.1 Library)
I have a View showing some data
<div class="display-label">Code</div>
<div class="display-field">
#Html.DisplayFor(model => model.Code).
</div>
Model.Code is HTML code for some Banners, I need to display the HTML on the page DECODED.
My question: How can I decode the HTML for just model.Code living the rest ENCODED?
Thanks for your help
To display raw html use
#Html.Raw(model.Code)
Be extremely careful though
you can use to output the data as is without encoding :
#Html.Raw(model.Code)
Use is #MvcHtmlString.Create(#Model.OurVision)

Resources