I’m building a C# MVC site where I can save code snippets. I use a textarea to paste in my code. It fails when I try to save certain characters. I’m using jqueryUI.
This is the function that saves my textarea to the DB where snippetAdd is the ID of my textarea:
function addSnippet() {
$("#newSnippet").show();
var myStuff = { snippetCode: $('#snippetAdd').val(), lexiconId: $('#lexiconId').val(), snippetDesc: $('#snippetDescAdd').val() };
var aURL = "/Lexicon/addSnippet";
$.post(aURL, myStuff, function(data) { parseMessage(data); }, 'json');
}
For example, if I past in and try to save the following, without the single quotes it fails:
‘<‘some code’>’
Thanks!
I believe that the problem is stemming from the fact that you aren’t encoding your input. (You didn’t elaborate on the error so this is a best guess.) Anyways, try doing this with all of your input:
This HTML-encodes your string, replacing special characters with their encoded equivalents. You will of course have to decode your string on the server side to reconstitute the original code entered in the field.