3
\$\begingroup\$

I have the following ajax call:

function getPreviewText() {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("PreviewWiki")',
        dataType: 'json',
        data: 'source=' + $('#markItUp').val(),
        success: function (data) {
            $('#previewMode').html(data.RenderedSource);
        }
    });
};

Controller action:

[HttpPost]
public ActionResult PreviewWiki(string source) {
    return Json(new { RenderedSource = m_wikiEngine.Render(source, GetRenderers()) });
}

And modal window:

enter image description here

I can switch between the design and preview tabs instantly which allows me to make changes in "Design Mode" and then instantly preview the effect of those changes before saving it to the Wiki. This works as I expect it to but I suspect that A. somehow I can accomplish this with get instead of post and B. I might not be going about this the right way.

Sure, it works exactly as I want it to but this is my first real web app and I'm confident that I'm "doing things right" here.

\$\endgroup\$
  • \$\begingroup\$ I can't vouch for the .NET code, but I don't see anything wrong with the JavaScript. You definitely don't want to use GET, because you'd have to include the data in the URL's query string (GET requests cannot have bodies), and then you're very likely to run into limits on the query string length. I know you've already implemented this with .NET for the rendering, but you might consider CodeMirror instead: codemirror.net \$\endgroup\$ – Johntron Jun 11 '13 at 2:03
4
\$\begingroup\$

Well there's not much code to review here, but for what's here, I'd say it looks okay. I would only make one suggestion. If all you're going to do in your controller action is return single string wrapped up in a JSON object, why not dispose of the JSON and just return the HTML as content?

Ajax call:

function getPreviewText() {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("PreviewWiki")',
        dataType: 'html',
        data: 'source=' + $('#markItUp').val(),
        success: function (result) {
            $('#previewMode').html(result);
        }
    });
};

Controller action:

[HttpPost]
public ActionResult PreviewWiki(string source) {
    return Content(m_wikiEngine.Render(source, GetRenderers()));
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you, this is exactly the sort of advice I was looking for. I originally wanted to return Content but settled for the JSON method since it was the only example I came across that I could get to work. \$\endgroup\$ – Kittoes0124 Jun 12 '13 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.