This is a migrated thread and some comments may be shown as answers.

Using async handling success event with an MVC controller

6 Answers 653 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Todd
Top achievements
Rank 1
Todd asked on 14 Apr 2012, 07:44 PM
Does anyone have an example of a controller method that will get the success event to fire? I keep getting the unexpected result error. 

My current controller method looks like this. I have tried many different results and nothing seems to work.
        [Authorize]
        [HttpPost]
        public String AddFile(int Id, IEnumerable<HttpPostedFileBase> attachments)
        {
            
            Response.ContentType = "text/plain"; 
            return   @"{""status"": 0, ""data"": [{""DocumentId"": ""5""}]}"; 
 
        }


        $("#attachments").kendoUpload({
            async: {
                saveUrl: saveURL,
                removeUrl: removeURL,
                autoUpload: true
            },
            upload: attachOnUpload,
            success: attachOnSuccess,
            error: attachOnError
        });


function attachOnUpload(e) {
 
        e.data = { Id: '@Model.Id' }
 
    }
 
    function attachOnSuccess(e) {
         
        var hiddenField = $("#DocumentId");
        hiddenField.val(e.DocumentId);
        
 
    }
    function attachOnError(e) {
        debugger;
	//always fires no matter what is returned from controller

    }

6 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 17 Apr 2012, 09:58 AM
Hello,

You should consider using the dedicated result type when returning JSON from an action method. This will save you the hassle of manually generating JSON.

[Authorize]
[HttpPost]
public ActionResult Save(int Id, IEnumerable<HttpPostedFileBase> attachments)
{
    // ...

    return Json(
        new {
            status = 0,
            data = new [] {
                new { DocumentId = "5" }
            }
        }, "text/plain");
}


I hope this helps.

Regards,
Tsvetomir Tsonev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Todd
Top achievements
Rank 1
answered on 17 Apr 2012, 02:05 PM
Thanks for the reply but that didn't help. I still get an error even though it looks like I am returning something valid.
0
Todd
Top achievements
Rank 1
answered on 17 Apr 2012, 10:48 PM
Success. I found out the issue was a javascript bug in my success handler. The error message still says unexpected result, so you may want to look into that, but that occurred because of an error in my code. Anyway, it is working. Thanks a lot.
0
James
Top achievements
Rank 1
answered on 23 Apr 2012, 09:07 PM
Why don't you just allow a response of HttpStatusCode.Created (or OK)?

Why does the event have to get json back? I don't care about anything other than it succeed....

Even when I return exactly what you specified above the success AND error events don't fire.

Here's the response:
Content-Type text/plain; charset=utf-8
{"status":200,"data":[{"DocumentId":"b553dbf6-5f99-4b4e-924b-a03c010c1a8c"}]}

Here's the actual script:

                <input id="upProfilePhoto" name="Photo" type="file" style="width: 150px" /> <input type="button" id="btnRemoveProfilePhoto" class="k-button" value="Remove" />
                <script>
                    $(document).ready(function () {
                        $("#upProfilePhoto").kendoUpload({
                            multiple: false,
                            showFileList: false,
                            async: {
                                saveUrl: "/members/profile/uploadphoto",
                                removeUrl: "/members/profile/removephoto",
                                autoUpload: true,
                                success: function (e) {
                                    debugger;
                                    $("#imgPhoto").attr("src", $("#imgPhoto").attr("src") + "?timestamp=" + new Date().getTime());
                                },
                                error: function (e) {
                                    debugger;
                                }
                            }
                        });
                    });
                </script>


Neither debugger get hit, and neither have any script errors in debug console in IE or chrome. (yes this is being run in a full web browser)
0
T. Tsonev
Telerik team
answered on 24 Apr 2012, 03:18 PM
Hello,

Providing a JSON response is strictly optional. The upload will fire the success if it receives empty response as well.

The only problem I see is that the success and error handlers are declared in the async configuration. Moving it to the root options should fix this.

All the best,
Tsvetomir Tsonev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
James
Top achievements
Rank 1
answered on 24 Apr 2012, 04:47 PM
Duh... thanks!
Tags
Upload
Asked by
Todd
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Todd
Top achievements
Rank 1
James
Top achievements
Rank 1
Share this question
or