I have a form on a webpage that I submit using the jquery form plugin. This form includes a file upload option. The MVC3 action that I post to returns JSON. Since the plugin falls back to using an iframe on older browsers you need to wrap your JSON with a
<textarea>JSON data...</textarea>
I tried changing the return type of the action to string and just appending the text area tags the the JSON object.ToString() but no go. How can I wrap my JSON result in a textarea when !Request.IsAjaxRequest()
Here is an example of me just trying to return the JSON as a string (which doesn’t work)
[HttpPost]
public string CreateEntry(EntryCreateViewModel model)
{
if (!ModelState.IsValid)
{
return WrapInTextArea(!Request.IsAjaxRequest(), Json(new object[] { false, 0, this.RenderPartialViewToString("_EntryCreateFormPartial", model) }).ToString());
}
This works in modern browsers but I suspect (based on the docs) will fail in older browsers that use an iframe
[HttpPost]
public ActionResult CreateEntry(EntryCreateViewModel model)
{
if (!ModelState.IsValid)
{
return Json(new object[] {false, 0, this.RenderPartialViewToString("_EntryCreateFormPartial", model)});
}
To solve this I inherited from JsonResult and added the textarea there
And then created a helper in my controller base class