views:

486

answers:

0

I have an ASP.NET MVC form that has a standard Submit button it and also a Preview button (so the user can see what an outgoing email will look like prior to actually sending it). The issue I'm running into is that the form allows users to upload files that will be sent with the outgoing email. I would like the Preview button to only grab the file names, not upload the files themselves, and the standard Submit to upload the file with the submission.

Here's how form is rendered to the client:

<form method="POST" enctype="multipart/form-data" action="/MVC/Action/URL">
    <input type="hidden" name="Class.List.Index" value="indexNumber" />
    <input type="file" name="Class.List[indexNumber].File" />

    <a class="previewLink" href="/MVC/Preview/URL">Preview</a>
    <input type="submit" value="Send Email" name="Send_Email" />
</form>

<script>
    $(document).ready(function() {
        $('.previewLink').SubmitAsAjax();
    });
</script>

and the script looks like this:

jQuery.fn.SubmitAsAjax = function() {
    if ($(this).attr('href') && $(this).attr('href').length > 0) {
        var href = $(this).attr('href');
        $(this).click(function() {
            var $form = $(this).closest('form');
            $form.ajaxSubmit({
                url: href,
                contentType: 'application/x-www-form-urlencoded',
                beforeSend: function() {
                    return $form.validate().form();
                },
                success: function(data) {
                    $('#PreviewResults').append(data);
                }
            });
            return false;
        });
    }
    return this;
};

I've tried setting the contentType in the Ajax call and I've tried removing and re-adding the enctype attribute. Neither seem to work. Has anybody encountered this and solved it before?