This is a migrated thread and some comments may be shown as answers.

(Un)Escape html rendering

1 Answer 17 Views
Form
This is a migrated thread and some comments may be shown as answers.
Frans
Top achievements
Rank 1
Veteran
Iron
Frans asked on 16 Mar 2021, 09:39 AM

Hi,

I am creating my own MVC Helper to wrap the Form component (serverside).

Inside that helper, I do something like this to create a custom html input element event:

StringBuilder sb = new StringBuilder();
string strSaveCall = $"SaveDetailItem(null, '{_formName}', '{_saveActionUrl}', null, null);"; // '/Home/Save'
string onkeyDownEvt = _htmlHelper.Raw("if (event.keyCode == 13) { " + strSaveCall + " }").ToHtmlString();

This renders the following html:

<input type="text" id="LastName" name="LastName" ...
onkeydown="if (event.keyCode == 13) { SaveDetailItem(null, &amp;#39;myForm&amp;#39;, &amp;#39;/Home/Save&amp;#39;, null, null); }" ....">

But I would like to see it rendered without the escaped characaters, like:
if (event.keyCode == 13) { SaveDetailItem(null, 'myForm', '/Home/Save', null, null); }"

How can I achieve this ?

Martin

 

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 19 Mar 2021, 08:54 AM

Hello Frans,

The ToHtmlString method returns an HTML-encoded string. You could try to decode it by using the HttpUtility/WebUtility HtmlDecode and or HtmlEncode to decode/encode the HTML string. You could check also the StackOverflow threads linked below where a similar issue is discussed:

- https://stackoverflow.com/a/15247805

 - https://stackoverflow.com/questions/23783679/how-to-convert-string-to-html-safe-string

- https://stackoverflow.com/questions/17682379/unable-to-html-decode-an-encoded-mvchtmlstring

- https://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c

I hope this helps.

Regards,
Neli
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Form
Asked by
Frans
Top achievements
Rank 1
Veteran
Iron
Answers by
Neli
Telerik team
Share this question
or