Consider the following example:
<div>#ViewBag.MyData</div>
What to do if ViewBag.MyData contains a string with "\n" (newlines) in it, that I want to change into "<br/>"? When I include "<br/>" instead of the newlines, it gets encoded in the browser, which is bad.
How can I help this?
use #Html.Raw(ViewBag.MyData.Replace("\n", "</br>"))
the following will do what you need...
#MvcHtmlString.Create(ViewBag.MyData.Replace("\n", "</br>"))
The Create method will take a HTML string and render it as would be expected if you just typed the HTML straight onto the page
That's what html pre tag is used for
The < pre> tag defines preformatted text. Text in a < pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks ...
And in razor template
< pre>#ViewBag.MyData</ pre>
Related
I have an HTML string that also contains a call to a partial view
When I render the string as such, it renders the HTML but not the Partial View.
My Partial file has the text Welcome to my Partial!
#Model.MyText = "This is my Text #Html.Partial('Partials/Partial') Done!";
#Html.Raw(Model.MyText)
Renders:
This is my Text #Html.Partial('Partials/Partial') Done!
Instead of:
This is my Text. Welcome to my Partial! Done!
How would I achieve this?
The razor snippet only works in the context of the .csharp file itself, not the rendered webpage. When you call #Html.Raw(Model.MyText), it correctly renders the string as html. However the resulting html is not parsed as C# code, it is just a string of text. This is not the way to achieve your desired result.
Everything you need done using C# must be done before the render. After rendering, its just html code/text.
I've searched over this website and I've looked over similar questions and i did not find the answer, I am sure it is somewhere but i did not find it, I have a string like this one for example :
string inputText = "<strong>Hello World</strong>"
This string comes from a certain request in control, and i have no power to change the model or the control. I can only change my razor view, using Html.Raw displays this result :
<strong>Hello World</strong>
And the result i want to be displayed is this one :
Hello World
How is it possible ?
PS: this is only a simple example, it can be any HTML Code.
You should use:
#Html.Raw(HttpUtility.HtmlDecode(inputText))
Decode and then render in html
To render any string (which includes HTML tags) -received from the model- as HTML, use:
#Html.Raw(Model.SomeString)
Please help me figure it out on this code, i want to build Common Html using TagBuilder with this code.
var href = new TagBuilder("a");
href.MergeAttribute("href", "#");
href.SetInnerText(item); <== This part rendered eg. "Just%20Test" but my text "Just Test"
i need your help to show "Just Test" because my text has white space.
i already try with Regular Expression but in my page always display like that if have white space.
Thank's in advice, and really sorry for my poor English.
Try using
href.InnertHtml = item;
in stead. According to the documentation, this will set the inner text without HTML encoding it first.
The funny thing is HTML encoding doesn't encode spaces... but I'm guessing there is more to the problem than your description. This might provide us with more information.
I have a asp.net page. On one particular page i can add comments and when i add these comments they get saved to a db. When i want new lines the "\n" tagg is automaticly displayed when i look at it in my backend code.
So now when i get back a response i want to get my comments back but i want them to have breaking spaces on those particular places where the "\n" is displayed.
I tried this in my backend code(cs):
Text = text.replace("\n", "<br/>")
the result i get:
my test<br/> test text new line<br/>
My <br> tags get displayed as text.
To clarify i basicly wan to achieve this:
Why are my "<br />" tags getting converted to "<br />"?
But from the backend code in C#.
Ive been googling and haven't been able to find a answer to this.
If you are using ASP.NET WebForms and you are putting the text in a Label control, this is normal behaviour. The Label control html-encodes its text before rendering it.
The Literal control does not use html-encoding, so if you change your Label to a Literal, it should work.
If you are using ASP.NET MVC, try using #Html.Raw(Model.Text).
You need to create an HtmlString with your content before printing it. So your code would be like
var foo = new HtmlString(text.replace("\n", "<br/>"));
and you would print foo.
I am using default asp.net ToolTip property, the text is
This is Going to be a long text
Thats why we decided to split it.
but using <br/> to split the line doesn't work, it renders as a text, and I want it to break the line instead.
Here is my code:
Label lblActionText = new Label();
lblActionText.Text = "Helloooo Phaltu";
lblActionText.Style.Add("cursor", "pointer");
lblActionText.ToolTip =
"This is Going to be a long text"
+ "<br/>"
+ "Thats why we decided to split it.";
I've not tried this but would "System.Environment.NewLine" not do the job instead of the BR tag?
You can use this to insert a line break inside a ToolTip.
lblActionText.ToolTip = " First text " + Environment.NewLine + " second text ";
Use the line break character entity (
). It is easy to implement but the only problem is that this will work in IE & Chrome but not Firefox.
lblActionText.ToolTip = "This is Going to be a long text
Thats why we decided to split it.";
See jsFiddle: http://jsfiddle.net/jafLf/
You are passing a string value to a Tooltip, and that's why the HTML element <br /> is not working.
However you can try this ASP.Net AJAX TooltipExtender
This works only on IE but other browsers are not!!
If you want to have a customize tooltip then seach for jQuery tooltips to have a formatted tooltip
You can do this with adding a table in the tooltip :)
Example: ASP forum
The ASP.NET ToolTip property corresponds to the HTML title attribute.
In IE and Chrome you can simply do:
<span title="multiline
tool
tip">Mouseover me!</span>
However, that won't work in Firefox as it actually follows the W3C guidelines for CDATA.
CDATA is a sequence of characters from
the document character set and may
include character entities. User
agents should interpret attribute
values as follows:
Replace character entities with characters,
Ignore line feeds,
Replace each carriage return or tab with a single space.
Your best bet (considering you also want to change the colour) would be to go with a jQuery solution such as QTip which gives you all the customisation you want and more..
You cannot change the color of a tooltip with the default title attribute. For that the only way is to use a javascript generated tooltip.
There are plenty of plugins for jQuery such as:
Dynamic tooltip
Popup Bubble
jQuery Horizontal Tooltips
Coda Popup Bubble
Awesomeness
TipTip
(mb)Tooltip
vTip
jGrowl
jQuery Ajax Tooltip
Digg-style post sharing tool with jQuery
Input Floating Hint Box
Simpletip
qTip
Orbital Tooltip
And many more, check them here: Stylish jQuery Tooltip Plugins Webdesign
You can also just create your own using javascript. Add a mouseover event on the element then show a hidden div over the element with whichever html elements you want, in whichever color you need.
It is also more safe to use a javascript approach for cross-browser compatibility.