ASP.NET MVC returning ContentResult using Ajax form - how to preserve whitespace?
Posted
by Ben
on Stack Overflow
See other posts from Stack Overflow
or by Ben
Published on 2010-05-12T15:01:34Z
Indexed on
2010/05/12
15:04 UTC
Read the original article
Hit count: 1529
asp.net-mvc
|asp.net-ajax
In my application users can enter commands that are executed on the server. The results are added to a session object. I then stuff the session object into ViewData and add it to a textarea.
When done with a standard HTML form whitespace is preserved.
However, when I swap this out for an ajax form (Ajax.BeginForm) and return the result as ContentResult, the whitespace is removed.
Controller Action:
[HttpPost]
public ActionResult Execute(string submitButton, string command)
{
if (submitButton == "Clear") {
this.CurrentConsole = string.Empty;
}
if (submitButton == "Execute" && !string.IsNullOrEmpty(command)) {
var script = new PSScript() {
Name = "Ad hoc script",
CommandText = command
};
this.CurrentConsole += _scriptService.ExecuteScript(script);
}
if (Request.IsAjaxRequest())
{
return Content(this.CurrentConsole, "text/plain");
}
return RedirectToAction("Index");
}
View:
<fieldset>
<legend>Shell</legend>
<%=Html.TextArea("console", ViewData["console"].ToString(), new {@class = "console", @readonly = "readonly"})%>
<% using (Ajax.BeginForm("Execute",
new AjaxOptions { UpdateTargetId = "console", OnBegin = "console_begin", OnComplete = "console_complete"}))
{ %>
<input type="text" id="command" name="command" class="commandtext" />
<input type="submit" value="Execute" class="runbutton" name="submitButton" />
<input type="submit" value="Clear" class="runbutton" name="submitButton" />
<%} %>
</fieldset>
How can I ensure that whitespace is preserved? When I inspect the response in FireBug it looks like the whitespace is transmitted, so can only assume it has something to do with the way in which the javascript handles the response data.
© Stack Overflow or respective owner