How to return error if JSON is being returned with data

6 posts, 1 answers
  1. Jaesoon
    Jaesoon avatar
    51 posts
    Member since:
    Oct 2012

    Posted 18 Jul Link to this post

    I've got a situation where I want to return data from the controller if there are errors on the file i'm uploading.

     

    For simple messages, i can just do [return Content("error")] which will cause the uploader to show an error message (with relevant error items - red colour, X icon etc).

     

    However, i have a case where i am returning a JSON object with details of all the issues with the uploaded file like so [return Json(returnItem)].

     

    I would expect the upload control to fail (as the documentation states it only accepts an empty string to be the success case) but i am getting the success behaviour from the upload control (green text).

     

    How can i make it so it shows the error state when i am returning a JSON object?

     

     

    Thanks

     

     

  2. Answer
    Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    278 posts

    Posted 20 Jul Link to this post

    Hi Jaesoon,

    Thank you for contacting us.

    By design, when the returned response is a JSON, it is interpreted by the Upload widget as a success response. The returned JSON is available deserialized in the success event handler and could be accessed by:
    function onSuccess(e) {
        var returnedObject = e.response;
    }

    Then, based on the content of the object sent, you could manually change the appearance of the file DOM element: 
    function onSuccess(e) {
        var returnedObject = e.response;
        if (returnedObject.ProductID == 56) {
            $('.k-file').removeClass('k-file-success').addClass('k-file-error');
        }
    }

    I hope that the above helps you. If you have any further questions, please do not hesitate to contact us.

    Regards,
    Veselin Tsvetanov
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jaesoon
    Jaesoon avatar
    51 posts
    Member since:
    Oct 2012

    Posted 20 Jul in reply to Veselin Tsvetanov Link to this post

    Thanks for your help Veselin,

     

    While it doesn't change control fully into the error state, trying to modify each elements manually to look like the error state will be too time consuming so we'll just stick with changing the colour to red.

     

    For your reference i've added an example, i1.png shows an error (with the alert icons) whereas i2.png shows a success (with the tick icon).

  5. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    278 posts

    Posted 21 Jul Link to this post

    Hello Jaesoon,

    You could further extend the custom styling so the failed file row looks like a true error row:
    function onSuccess(e) {
        var returnedJson = e.response;
        if (returnedJson.ProductID == 56) {
            $('.k-file').removeClass('k-file-success').addClass('k-file-error');
            var statusStrong = $('.k-upload .k-file .k-upload-status');
            statusStrong.find('.k-upload-pct').removeClass('k-upload-pct').addClass('k-icon k-warning').text('');
            setTimeout(function () {
                statusStrong.find('.k-icon').removeClass('k-i-close').removeClass('k-delete').addClass('k-i-refresh k-retry').attr('title', 'Retry');
            }, 200);
        }
    }

    I hope that this helps you.

    Regards,
    Veselin Tsvetanov
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Tamas
    Tamas avatar
    1 posts
    Member since:
    Feb 2015

    Posted 07 Oct in reply to Jaesoon Link to this post

    You can also set the status code to 500, then return the whatever JSON you want:

    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    return Content(JsonConvert.SerializeObject(new { Errors = errors }));
               

  7. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2153 posts

    Posted 07 Oct Link to this post

    Hello guys,

    As of Q3 2016 the success event of the Upload widget is preventable, which will result in displaying the file as failed. This is helpful in scenarios like the current one, when a JSON data should be returned in any case.

    Regards,
    Dimiter Madjarov
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready