I have a string like
"For more information, go to google"
When I directly put this string inside a span in my razor (cshtml) view, I get the raw string in the DOM. How can I make it behave like HTML content, i.e. text followed by a link? Is there any way other than parsing the string and extracting the href and link text and then putting them inside a new anchor tag?
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 have an HTML string in my code behind which has some valid html tags such as <br/>, <p></p>, and some invalid tags such as <test>. I want to render both sets of tags in the browser in such a way that invalid tags are rendered as plain text.
For example, the string
<test><br/>hi mark.<br/>how are you.My email is <test#test.com>
would to need to output on the browser as
<test>
hi mark.how are you.My email is <test#test.com>
You would need to whitelist the elements you consider to be valid, and then encode those which do not match the whitelist so that they are instead converted to <test> as opposed to <test>. The ampersand values will render as < text on the page.
I am trying to get the string output of Html.BeginForm() as a string inside a view, without printing it in the view. Is it possible?
EDIT >
I am trying to implement some text helpers to work with html tags inside C#, such ass:
using(Html.BeginText("p")){
Hi, my name is Ashley. I love
using(Html.BeginText("small")){
small letters
}
}
I used IDisposable, so I don't have to worry about the closing tags and I can insert html text inside the brackets, but I want to get this values as string, not to print it to the user.
I am trying to create a string in such a way that it should rendered as anchor tag in cshtml page.But instead of anchor tag, it is rendering as string and it shows the string as it is. Can somebody correct me please.
Here is what I am doing
string info= "You are on this page. <a href=www.yahoo.com target=_blank >Click to view</a>"
Having a list you can loop over the list and then use Html.Raw to display the strings as raw HTML:
#foreach(var link in list)
{
Html.Raw(link);
}
WebUtility.HtmlEncode("<b>This html format<b>");
I have a simple string variable that contains a portion of HTML inside. For example:
string contents = "<div><p>Hi how are you. Click here if you want to know more";
I want to include this HTML in page:
<div class="description">
#contents
</div>
However, it messes up the rest of the page because of unclosed tags.
Is there a function (or a helper) that reads and formats the HTML inside for example, to complete the HTML without errors:
#Html.DisplayProperHTML(contents)
This will render as:
<div><p>Hi how are you. Click here if you want to know more</p></div>
There is no such functionality built-in.
You can use the HTML Agility Pack to parse and fix broken HTML.