Using async handling success event with an MVC controller

7 posts, 0 answers
  1. Todd
    Todd avatar
    13 posts
    Member since:
    Dec 2011

    Posted 14 Apr 2012 Link to this post

    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
    
        }
  2. T. Tsonev
    Admin
    T. Tsonev avatar
    2834 posts

    Posted 17 Apr 2012 Link to this post

    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!
  3. Todd
    Todd avatar
    13 posts
    Member since:
    Dec 2011

    Posted 17 Apr 2012 Link to this post

    Thanks for the reply but that didn't help. I still get an error even though it looks like I am returning something valid.
  4. Todd
    Todd avatar
    13 posts
    Member since:
    Dec 2011

    Posted 17 Apr 2012 Link to this post

    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.
  5. James
    James avatar
    20 posts
    Member since:
    Apr 2012

    Posted 23 Apr 2012 Link to this post

    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)
  6. T. Tsonev
    Admin
    T. Tsonev avatar
    2834 posts

    Posted 24 Apr 2012 Link to this post

    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!
  7. James
    James avatar
    20 posts
    Member since:
    Apr 2012

    Posted 24 Apr 2012 Link to this post

    Duh... thanks!
Back to Top