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

Return metadata from controller on success?

1 Answer 824 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Iron
Tom asked on 25 Aug 2017, 03:05 PM

I have an Upload element that needs to receive data from the controller upon a successful upload, but I can't seem to find a way to make this work. My Upload looks like this:

@(Html.Kendo().Upload()
    .Name("FileUploader")
    .Multiple(false)
    .Async(async =>
        async.Save("ReplaceDocument", "Library",new { DocumentId = Model.Id})
        .AutoUpload(true)
    )
    .Events(e => e
        .Success("UploadSuccess")
    )
)

This saves to an action called ReplaceDocument in my Library controller, and includes the DocumentId from the Model. This all works great.

...but that ReplaceDocument action needs to return some data to the page. I understand that in the simplest version of this scenario, the action should simply return a string with an error message, or just an empty string if the upload is considered a success. I need to return more data. But how?

If I amend the ReplaceDocument action, and make it an ActionResult, I can return a json:

return Json(new { status = "OK", LastUpdatedUTC = lastUpdated }, "text/plain");

...or perhaps:

return Json(new { status = "Access denied.", LastUpdatedUTC = lastUpdated }, "text/plain");

I can retrieve this response data from the UploadSuccess js function. The second example given above should obviously indicate a fail state to the user, but I cannot find a way to tell the Upload element when the file was not accepted.

Is there something I am missing? How do I trigger a fail state? Or is there a better way to receive data from the save Controller?

1 Answer, 1 is accepted

Sort by
0
Tom
Top achievements
Rank 1
Iron
answered on 25 Aug 2017, 03:17 PM

Nevermind, figured it out.

With the controller action as an ActionResult, a fail state is returned with:

return Content("Error Message Here");

And a success state can be returned with either the same, but with an empty string, or any JSON.

Tags
Upload
Asked by
Tom
Top achievements
Rank 1
Iron
Answers by
Tom
Top achievements
Rank 1
Iron
Share this question
or