Html Encode on a string with symbols - asp.net-mvc

How do I encode a string with symbols like ', & # #' in ASP.Net MVC?
I have tried to use Html.Encode, but it returns '&#39', how do I return a string as the user input?
Thanks alot.

If you intend to insert the string into the HTML markup then you need to HTML encode it:
<%= Html.Encode("',&##'") %>
or if you are using ASP.NET 4.0:
<%: "',&##'" %>
Doing this will properly encode any characters in the string.

Related

Rails tag syntax with multiple data attribute values quotes excluded

Trying to generate
<img data-interchange="[assets/img/interchange/small.jpg, small], [assets/img/interchange/medium.jpg, medium], [assets/img/interchange/large.jpg, large]">
is challenging.
<%= tag "img", data: { interchange: ["[#{photo.image_url(:base).to_s}, small]", "[#{photo.image_url(:big).to_s}, medium]", "[#{photo.image_url(:desktop).to_s}, large]"] } %>
will generate HTML, but the result is still inoperative
<img data-interchange="["[https://[...]base_20160529_115006.jpg, small]","[https://[...]big_20160529_115006.jpg, medium]","[https://[...]desktop_20160529_115006.jpg, large]"]" />
as " gets generated.
What is the proper syntax to get the proper HTML string
In rails you can use ActiveSupport's to_json to encode things into json.
<%= tag "img", data: { interchange: [[photo.image_url(:base), 'small'], [photo.image_url(:big), 'medium'], [photo.image_url(:desktop), 'large']].to_json } %>
Which produces
<img data-interchange="[[\"assets/images/interchange/base.jpg\",\"small\"],[\"assets/images/interchange/big.jpg\",\"medium\"],[\"assets/images/interchange/desktop.jpg\",\"large\"]]" />
Then on the front-end you can use JSON.parse once you retrieve the value via getAttribute or with jQuery data, if you need to use the data on the front-end.
var text = JSON.parse(element.getAttribute('data-interchange'));

Ruby .to_sentence

In the .to_sentence method, How can I get the last_word_connector to output an HTML string?
For example:
locations.to_sentence last_word_connector: '<br />'
I want the <br /> to actually be interpreted. I have tried adding .html_safe on the end but to no avail.
You'll need to tell the view that the string is "safe" to insert (i.e. it should be inserted as-is - not escaped):
<%= locations.to_sentence(last_word_connector: '<br />').html_safe %>
The html_safe method returns a string that's marked as, well, html-safe. So it won't be escaped.
Use with caution. The reason Rails escapes string by default is that it makes html- and javascript-injection attempts harmless. So if you're not completely sure of the content of locations, then be careful.

Escape ruby code inside HTML string

I have an object in my controller
#data="<meta property=\"og:url\"
content=\"<%=SITE_URL%><%=request.request_uri%>\"
/>".html_safe
When I print this in my view file by <%=#data%> the html that it generates is
<meta property="og:url"
content="<%=SITE_URL%><%=request.request_uri%>"
/>
How do i print out/escape the ruby code withing the string?
You're looking for string interpolation, which means that in the string, it substitutes the value of the variable. Surround the variables with #{} like this:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
#data="<meta property=\"og:url\"
content=\"#{SITE_URL} #{request.request_uri}\"
/>".html_safe
Now, when you do <%= #data %>, it should show up normally.
#data="<meta property=\"og:url\"
content=\"<%= #{SITE_URL}%><%= #{request.request_uri} %>\"
/>"
In your views use raw helper method
<%= raw(#data) %>
You can try h helper like this.....
<%=h(#data) %>
also raw can be use
<%=raw(#data) %>

Need Help on a custom HTML Helper

I'm trying to create a custom HTML Helper to help simplify my masterpages menu, however it is not rendering on the HTML when I use it.. I'm thinking I will need to create a partial view, any ideas?
I did this..
public static string CreateAdminMenuLink(this HtmlHelper helper, string caption, string link)
{
var lnk = TagBuilder("a");
lnk.SetInnerText(caption);
lnk.MergeAttribute("href", target);
return lnk.ToString(TagRenderMode.SelfClosing);
}
Now in my View, i have
<% Html.CreateAdminMenuLink("Home", "~/Page/Home"); %>
Thanks: Dave Swersky
Fix was: I forgot the equals and removed the semi-colon
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
but when I look at the source, its empty.. tried adding <% using (Html.BeginForm()) %> and it adds a form.. but the link still doesnt come up.. debugged and the string works when i look at the watch, but does not render..
Any ideas?
Modify your markup:
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
The equals sign and no semicolon should do the trick.

Dynamic RadEditor Creation through HtmlHelper

I am using the Telerik RadEditor (Q1 2009 SP1) in our ASP.NET MVC (RTM) project. The editor works great when rendered as a hardcoded object on the page with a static id. But when extending with an HtmlHelper to do dynamic creation by passing in an Id it seems to render the html as all lowercase for the tag. Does the HtmlHelper object mess with this innately by chance? The attributes look upper and lowercase respectively but this seems strange. Here is my code....thanks in advance!
<% if (placeholder.Type.ToLower() == "richtext") { %>
<%= Html.RadEditor("placeholder_" + placeholder.Name) %>
<% } else { %>
<%= Html.TextBox("placeholder_" + placeholder.Name, null, new { #class = placeholder.Type }) %>
<% } %>
The helper looks like this....
public static string RadEditor(this HtmlHelper html, string Id)
{
var sb = new StringBuilder();
sb.Append("<telerik:RadEditor ID='" + Id + "' Runat='server' DialogHandlerUrl='~/Telerik.Web.UI.DialogHandler.axd'>");
sb.Append("<Content>");
sb.Append("</Content>");
sb.Append("</telerik:RadEditor>");
return sb.ToString();
}
For the time being you cannot render RadEditor without having a valid Page object with a ScriptManager. We (Telerik that is) plan to add support for "standalone" rendering in the near future. Should be announced in a blog post so stay tuned.
The problem is the tag is a server side control. When you place it hardcoded in your page, the server side tag gets translated to html. When you're using the htmlhelper, you're outputting the html and it doesn't get processed as a server side tag.
If you want to do something dynamic, you should use a UserControl (.ascx file) and then use the Html.RenderPartial method.

Resources