I have an application that tests a user's ability to code. The textarea I am using to capture their code is by codemirror. I have a CodeTemplate for certain questions which is stored in my database. An example stored CodeTemplate is: using System;\n\nnamespace Math\n{\n\tclass Math\n\t{\n\t\tstatic void Main(string[] args)\n\t\t{\n\t\t\t\n\t\t}\n\t}\n}
which should theoretically print out:
using System;
namespace Math
{
class Math
{
static void Main(string[] args)
{
}
}
}
This is what it actually prints:
using System;\n\nnamespace Math\n{\n\tclass Math\n\t{\n\t\tstatic void Main(string[] args)\n\t\t{\n\t\t\t\n\t\t}\n\t}\n}
============================
Here is my Controller:
public ActionResult Evaluation()
{
EvaluationPackage tester = new EvaluationPackage();
Console.WriteLine("Testing Receiving Evaluation Package");
var result = EvaluationPackageService.GetQuestions(11);
// Put in question titles
ViewData["question1"] = result[0].Text;
ViewData["question2"] = result[1].Text;
ViewData["question3"] = result[2].Text;
// Put in code wrapper
ViewData["codeWrap1"] = result[0].TemplateCode;
ViewData["codeWrap2"] = result[1].TemplateCode;
return View();
}
============================
Here the problem section of my View:
<div class="question-header">
<h2 class="problem-title">Question 1 - C# Coding</h2>
<button type="button" class="problem-status btn btn-outline btn-warning-outline answer-indicator">No answer yet</button>
</div>
<p class="question">@ViewData["question1"]</p>
<div class="question-response">
<form action="Test/Compile/" method="post">
<textarea id="cs-code" class="code-area">
@Html.Raw(@ViewData["codeWrap1"]) <!-- HERE IS THE PROBLEM -->
</textarea>
</div>
============================
I've already tried using @Html.raw
and removing @Html.raw
, and I have tried replacing the \n
with <br>
, but alas no luck.
Any suggestions?
The database INSERT was:
using System;\n\nnamespace Math\n{\n\tclass Math // ETC
The database SELECT was:
using System;\\n\\nnamespace Math\\n{\\n\\tclass Math // ETC
It was escaping the slash rather than the n. To fix this, the INSERT became:
'using System;' + CHAR(13) + CHAR(13) + 'namespace Math' + CHAR(13) + '{' // ETC
CHAR(13)
is for \n
, and CHAR(9)
is for \t
.