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())
Related
I have a block of HTML I need to render in multiple places on a page, and I'm looking for a way to only define that HTML once. I can't rely simply on a loop because the HTML appears in different areas.
I know I can use a partial view. But since the block of HTML will only be displayed one one page, I'd prefer to define it there.
I know I can create a #functions block to create a function to render the markup, but this is geared towards code and not markup. I'd like something more like #helper functions in MVC, but those don't appear to be available in Razor Pages.
Can anyone offer other suggestions for defining a block of HTML in one place so it can be shown anywhere on the page?
If you are working with .NET Core 3, you can include HTML tags in methods declared in an #functions block e.g.
#functions{
void Greeter()
{
<h3>Hello World</h3>
}
}
Then in the content part of the page:
#{ Greeter(); }
The kind of helper can also take parameters:
void Greeter(string greeting)
{
<div>#greeting World</div>
}
#{ Greeter("Hello"); }
If you are working with ASP.NET Core 2.x, your "helper" method is a Func<someType, IHtmlString>. In the following example, the someType is a string:
Func<string, IHtmlContent> Greeter = #<h1>Hello #item</h1>;
Then in the content part of the page:
#Greeter("World");
someType can be a complex type:
Func<Contact, IHtmlContent> Greeter = #<h1>Hello #item.FirstName</h1>;
Template tag can help:
<template id="block-template">
<div>
<p>template contents...</p>
</div>
</template>
<div id="target1"></div>
<script>
var tmplt = $("#block-template").html();
$("#target1").append(tmplt);
</script>
the template tag is available in HTML5, you can also use script template :
<script id="block-template" type="text/template">
<div>
<p>template contents...</p>
</div>
</template>
there is a lot of plugins to use if you need to bind data to the template :
http://handlebarsjs.com/
https://github.com/jcgregorio/stamp/
https://knockoutjs.com/documentation/template-binding.html
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
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>
I have this problem: From a database, held up a string, which contains HTML mixed with C# code. I wish I could run correctly both codes on my page .aspx.
e.g.
in my .aspx:
<div><%= Model.repo.getCode() %></div>
and the getCode() method give me this:
<div id="secondDiv"><p><%= Model.Person.Name %></p></div>
so I want the final html file look like:
<div><div id="secondDiv"><p>Jhon</p></div></div>
any suggestion?
There may be direct way to bind such value,
But if you could store String.Formatable into database then it would be easy to bind the data needed.
Using String.Format you achieve like,
returned string from Model.repo.getCode() (see curly braces)
"<div id="secondDiv"><p>{0}</p></div>";
And in ASP code,
<div><%= string.format(Model.repo.getCode(),Model.Person.Name) %></div>
Take a look at this project as it helped me with a similar problem: https://github.com/formosatek/dotliquid Basically you can bind whatever objects to a template and that template can call properties of you objects and even use conditional logic and loops.
I am having issues with the output of my C# script embedded in my asp.net code. The output is generated after clicking a submit button for a web form. This web form is at the top of the page. The output, when clicking submit, is currently being placed above the web form which is in turn pushing the web form underneath it. I would like the opposite to happen. I want it to output below my web form. The way I generate output from my script is as follows:
Response.Write("<p>");
foreach(obj in arr){
Response.Write(obj);
}
Response.Write("</p>");
Also if it matters, I initialize the script with runat="server". The script gets called when the user selects "submit" near the web form. Thanks in advance. I've been trying to format this thing for quite some time now.
You would be better off putting a 'literal' object in the place on your page precisely where where you want the result to appear, and then, instead of spitting out HTML with response.write, you assign the desired text to the literal in your code-behind.
Like this:
<html>
<p>
<asp:Literal ID="ltlTest" runat="server"></asp:Literal>
</p>
</html>
and then in your code behind:
ltlTest.Text = "the string you want to show...";
You can include html tags in the string assignment, though generally I try not to.
You've got some choices.
You can make arr a public property, and then use <% foreach (var obj in arr) Response.Write(obj); %> directly in the page markup where you want it.
You can put in an <asp:Literal runat="server" ID="Literal1"> control and then set Literal1.Text = ... in your code. This achieves the same, but with ViewState (so the value is persisted on postbacks).
If you'd like the result to be rendered within <span /> tags, you can use an <asp:Label /> control. This is usually the best choice for displaying messages to the user.
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.