Submitting a multipart AJAX form with CKEditor textarea

OK, the subject of this post is pretty niche, but that’s why I am posting about it because I do it so little I’d only forget about it for the next time!

With ASP.NET MVC if you want to allow a file upload on a form submitted by AJAX then you have to intercept the form submit event and do the submit it yourself via Javascript, rather than using MVC’s helpers. This is detailed in this useful Stackoverflow answer: How to do a aspnet mvc ajax form post with multipart form data

What the post doesn’t mention (as it wasn’t asked for!) was that if your form contains an instance of the popular rich text editor CKEditor then this code alone will not transfer the data the editor contains to the form. I think this must be because the CKEditor is waiting for the submit event to be triggered before it does the copy into the field.

It took a bit of digging, as I have not had to look at the CKEditor API before, but if you take the code from the above link as a start point, then add the following code to somewhere between the event.preventDefault() call and the ajax() call, it seems to work OK.

            for (instance in CKEDITOR.instances) {

Adding CKEditor to an ASP.MVC AJAX dialog

CKEditor is one of the most popular rich text editors for the Web out there, but although I’ve used it many times I’ve never added it to an MVC site before. Although following the instructions is simple enough for a static view, adding it to a dynamically created view within a jQuery dialog box proved a little bit trickier. As usual there are bits and bobs around on the Net about it, but never the whole thing in one place! So I’ve collated what I’ve found, and added some bits I discovered myself.

OK, so say you want to popup an Edit screen in a jQuery dialog, generating the Edit view to go within the dialog on-the-fly. Download the CKEditor and put the scripts into your site, it doesn’t matter what configuration you use. I usually just put it under the Scripts directory. Also download the jQuery plugin jquery.livequery and put it into your site.

You need the following script links in your page header. whatever.js is just the name of a JS file that you are going to create in a minute, obviously change the paths of the CKEditor and livequery scripts if necessary. It is crucial to include the jQuery adaptor.

<script src="@Url.Content("~/Scripts/jquery.livequery.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ckeditor/ckeditor.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ckeditor/adapters/jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/whatever.js")" type="text/javascript"></script>

To start with you would put an Ajax.ActionLink on your View with a call to the Edit screen a bit like this (Razor view syntax):

@Ajax.ActionLink("Edit", "Edit", "Whatever", new AjaxOptions { UpdateTargetId = "editWhatever", OnBegin = "showEdit()"})

Where editWhatever is just an empty div somewhere else on the page, I’ll come onto the showEdit() Javascript in a minute.

The Edit view (and view model) would be something like below. Note that for some reason your field must be an Html.TextArea or TextAreaFor, a regular HTML textarea tag does not work, even though it is fine to use that tag in a non-AJAX form.

@model MyProject.ViewModels.WhateverEditViewModel

@using (Ajax.BeginForm("Save", "Whatever", new AjaxOptions { UpdateTargetId = "editWhatever", OnSuccess = "hideEditOnSuccess()" })) 
    @Html.HiddenFor(m => m.Site.Code)

    <div class="question-editor-label" >
       @Html.LabelFor(model => model.WhateverPropertyToEdit)
    <div class="section-editor-field" >
            @Html.TextAreaFor(model => model.WhateverPropertyToEdit)

    <button title="Save" type="submit" value="Save"  >Save</button> 
    <button type="button" value="Cancel" onclick="doCancel()" title="Cancel" >Cancel</button>

And you could probably work out the view model, but here it is:

namespace MyProject.ViewModels
    public class WhateverEditViewModel
        [Display(Name = "Whatever Description")]
        public string WhateverPropertyToEdit { get; set; }

And remember the ‘Save’ controller action method needs to accept HTML, so you have to decorate it with the annotation [ValidateInput(false)].

public ActionResult Save(WhateverEditViewModel passedModel)

Below is the Javascript for the page contained in whatever.js. In the $(document).ready() function it is crucial to use livequery to detect the appearance of the field on the edit form, as it does not exist when the document is first created. Use the jQuery call to ckeditor() to launch CKEditor. The rest should hopefully be obvious.

$(document).ready(function () {

    $('#WhateverPropertyToEdit').livequery(function () {

function showEdit() {
            modal: true,
            width: 1000,
            title: 'Edit Whatever',
            dialogClass: 'edit_whatever_class',

function doCancel() {

function hideEditOnSuccess() {
    if ($('#editWhatever').find(".validation-summary-errors").size() == 0) {
        /* do anything to the 'host' screen you need to here. */

And you should now see the editor appear on your dialog box.

Custom stored procedures returning DataSet instead of TList in NetTiers – and how to change!

When we upgraded our Nettiers installation on an old website recently some of our custom stored procedures suddenly started to return a DataSet rather than a TList. This is understandable – the stored procs were complex and so Nettiers wouldn’t have been able to guarantee the return type. Probably the earlier version of Nettiers was wrong to trust us in the first place. However *we* knew that it would always return the correct thing, and there were far too many places in the code to go back and recode it all to cope with a Dataset.

It seemed tricky to find the answer so I’ve reposted it here from the original source (

Set the Codesmith template setting AllowCustomProcMultipleResults (under the CRUD -Advanced section) to True and then regenerate. This seems to tell Nettiers to just trust that you know what you are doing, and sets the return type to that of the entity in the provider.

FileUpload control with UpdatePanel (WebForms)

The asp:FileUpload control rather irritatingly does not work within an UpdatePanel in ASP.NET Webforms. This is probably something to do with security (which always seems to be an issue with AJAX!).

To get round this problem you can of course take it out of the UpdatePanel and have it on a separate form, but that is not really that slick, and I expect the main reason you are trying to do this is for slickness!

The solution is to put the upload control and associated upload functionality on another aspx page, and then include the page within your form using an iframe with the frameborder property set to zero. Of course that is not perfect, as you will need to provide a separate button for uploading, which might be irritating to the user. On the other hand that also allows you to display things like photo uploads relatively easily and apparently seamlessly.

Your main problem will be giving the upload page the information about where to store the file/photo (if it associated with a specific data record). When updating a record this can be passed as a parameter to the page request. Adding a record is more tricky. In this case if you generate your data’s key prior to saving then no problem, but if you allow the database to do it after saving it is a problem, as you can’t pass it through to the upload page! In this scenario the best process flow might be not allow file uploads in insert mode – just ask the user to save the data record, then have the page automatically go into edit mode afterwards for photos.

It all feels a little bit hacky but sometimes that is just the way AJAX works!