I am using C# ASP.NET MVC, and wish to populate a DIV with html stored in a database. I have tried the following:
//getting a string of HTML from a database
#{
string myHtml = Model.Data.Html;
}
//Using Javascript to insert this into a Div
<script>
$("#myDiv").html("#myHtml");
</script>
The div fills with the html, but it is inserted as raw text and displays all of the html tags. I have tried .append and .html to no avail.
Any help would be appreciated.
Maybe you want to use the #Html.Raw() method?
$('#myDiv').html('#Html.Raw(myHtml)');
Be careful as this could be part of an XSS vulnerability. Don't directly render user input as unencoded HTML. (This is exactly why the ASP.NET MVC Framework HTML-encodes output by default, which is the issue you're seeing.)
Try
$("#myDiv").html('#Html.Raw(Model.Data.Html)')
If you don't have an explicit requirement to use Javascript to do this, simply use Html.Raw to output unencoded HTML inline.
<div id="myDiv">#Html.Raw(myHtml)</div>
Related
I'm creating some html code from a cs file, which i want to be rendered one the actual site.
But what I get rendered is the "raw" html code, not the rendered. What am I doing wrong?
the .cshtml code:
<div>
#class1.createhtml();
</div>
the .cs code:
public static string createhtml()
{
return "<p>hi</p>";
}
<div>
#Html.Raw(class1.createhtml())
</div>
You should be very careful that you don't put unescaped user-input into this string, or you open up the door to injection attacks - treat this with the caution that you might treat appending strings to make a SQL query: avoid it if at all possible, or be very careful.
You should use Html.Raw method in your view:
#Html.Raw(class1.createhtml())
I create a String in a view and want to output it to the screen.
Initially I tried Response.Write but, due to reasons explained elsewhere on this site, the content appeared at the top of the page. I then attempted to output the string using # like so: #myString.
This worked, in that it output the text at the right location but it escaped HTML links that I had put in there. How do I get around this problem?
You can use Html.Raw():
#Html.Raw(MyStringVar)
Instead of string you will want to use:
#{
var myString = new MvcHtmlString("<tags>Text</tags>");
}
Then when you inline render it:
#myString
It'll display the correct value instead of escaped text.
Edit: Alternative
The other option is you can just create the MvcHtmlString inline so it renders appropriately.
#(new MvcHtmlString(myString))
You can use the Html.Raw function for this
For example:
#Html.Raw(Model.YourString)
#model myString
#Html.Raw(Model)
Try #Html.Raw(myString).
I do not recommend doing this. You don't send html output to screen for various reasons. if you want to put the text in certain part of the screen, use html/css styling in the view and send only the output.
You could do it this way:
#Html.Raw(myString)
Basically i have a webpage with embedded css and JavaScript, so what i want to do is extract only the HTML itself, from texts to tables , images and what not.
So far i have the whole web page stored into a string called "html" the contents of this page is just the facebook hompepage for example,but as you will see there's all scripts and other embedded stuff which i don't want to have.
HTMLEdit = //webpage I chose to store in here//
string html = HTMLEdit.DocumentText;
String result = "this i want to only contain the <head>,<body>,<foot>."
I am only interested in displaying the result witch only contains html, i don't want the JavaScript or css or any other stuff
I have looked at the agility pack but there's no documentation on there website to do this and this is my first ever c# project i have decided to make, so excuse my ignorance if i don't make sense.
See this question
HTML Agility Pack strip tags NOT IN whitelist
Maybe adapt that answer, and drop link and script tags.
I am trying check if the inner html of the element is empty but I wanted to do the validation on the server side, I'm treating the html as a string. Here is my code
public string HasContent(string htmlString){
// this is the expected value of the htmlString
// <span class="spanArea">
// <STYLE>.ExternalClass234B6D3CB6ED46EEB13945B1427AA47{;}</STYLE>
// </span>
// From this jquery code-------------->
// if($('.spanArea').text().length>0){
//
// }
// <------------------
// I wanted to convert the jquery statement above into c# code.
/// c# code goes here
return htmlSTring;
}
using this line
$('.spanArea').text() // what is the equivalent of this line in c#
I will know if the .spanArea does really have something to display in the ui or not. I wanted to do the checking on the server side. No need to worry about how to I managed to access the DOM I have already taken cared of it. Consider the htmlString as the Html string.
My question is if there is any equivalent for this jquery line in C#?
Thanks in advance! :)
If you really need to get that data from the HTML in the ServerSide then I would recommend you to use a Html-Parser for that job.
If you check other SO posts you will find that Html Agility Pack was recommended many times.
Tag the SpanArea with runat="server" and you can then access it in the code behind:
<span id="mySpan" class="spanArea" runat="server" />
You can then:
string spanContent = mySpan.InnerText;
Your code-behind for the page that includes this AJAX call will have already have executed (in presenting the page to the browser) before the AJAX call is ever executed so your question doesn't appear correct.
The code-behind that is delivering the HTML fragment you indicated is probably constructing that using a StringBuilder or similar so you should be able to verify in that code whether there is any data.
The fragment you provided only includes a DIV, a SPAN and a STYLE tag. This is all likely to collapse to a zero width element and display nothing.
Have a look at this article which will help you understand the ASP.NET page life cycle.
I have a ASP.NET 4 page and I am using tinyMce.
My aim is to change toolbar setting for tinyMce depending on "Role" associated to logged User.
To solve it my idea is to include different version of tinyMce JavaScript code using some logic.
My questions are:
- do you know a better solution?
- how to include JavaScript inside the Head of the page pro grammatically?
Thanks
It sounds as if you're not using MVC with MasterPages (which has a built in method for including such a JavaScript). If you're using web forms, you can put a Literal in the head of your document and assign its text value from the code behind:
<asp:Literal ID="myTinyMCEScript" runat="server" />
...
string adminScriptText = "some javascript to format for admins";
string userScriptText = "some javascript to format for knuckledraggers";
this.myTinyMCEScript.Text = (myUserRole == "admin") ? adminScriptText : userScriptText
Not terribly elegant, but effective.
Edit:
To use a file:
string adminScript = "<script language=\"javascript\" type=\"text\\javascript\" src=\"link/to/adminscript.js\"></script>";
string userScript = "<script language=\"javascript\" type=\"text\\javascript\" src=\"link/to/userscript.js\"></script>";
Since you want to store it in a file, and javascript is run in plain text at the browser, you might as well just stick it in a js file and include that js file.