I am using the igGrid using the Grid Model approach. After Updating/Editing the grid post to an MVC controller. I would like an example how to handle Server Side errors and displaying the error to the returned View.
Thanks
Hello Eric,
I am glad that you find my answer helpful.
Feel free to contact me if you have some further questions regarding this scenario.
Vasil,
Thank you very much for your help and clarification.
Eric
The reason a dictionary<string, bool> is used, is because “Success: true” has to be added to the response to the client – the igGrid relies on it in order to execute the “success” callback of the “saveChanges” method. Without this property, the error callback (if you have such) would be executed every time.
This help topic provides a more detailed code sample and explanation.
I have attached a small MVC sample that has a simple igGrid, which sends a request to the server by calling “saveChanges” when a "save" button is pressed. Try adding or editing a row and once you press the button, the “success” handler would be executed. If you try to delete a row, the server would not allow it and would return an error message, which would be shown to the client.
MVCServerReponses.zip
Let me know if you need some additional information with regard to this topic.
A couple more questions..
try { //DO Something } catch (Exception ex) { response.Add(ex.ToString(), true); result.Data = response; return result; } response.Add("Success", true); result.Data = response; return result;
I this code I am trying to return the Exception message. This doesn't seem to work. I am obviously missing something. How does the igGrid take in a custom error message? What I receive back, with the following code is this...
$("#saveChanges").bind({ click: function (e) { $("#PayIncreasePreview").igGrid("saveChanges", function (data) { window.location.href = '@Url.Action("Index", "MassGlobalUpdate")'; }, function (jqXHR, textStatus, errorThrown) { console.log(textStatus); console.log(errorThrown); }); return false; } });
Console:
Saving changes was not successful. Server did not return Success object or returned Success:false
What is the best way to send the Exception message back to the client. And could you explain or point me to documentation why the JSON response should be a dictionary<string,bool> and what it means to send back true and false?
The grid API provides the scrollContainer method which would give you the scroll container – it would allow you set the scrollTop to zero and scroll the grid to the top when there is an error – that is exactly what these two lines of the snippet below do:
let container = $('#grid').igGrid('scrollContainer')[0]
container.scrollTop = 0;
Setting the window.location.href to the according URL would allow you to redirect to another page if the server response is successful:
window.location.href = "">https://www.someotherurl.com"
You could then use success and error callback functions, which the “saveChanges” method would execute when the server responds back to the client. In case your popup has an id of “message”, the code might look like this one:
$("#grid").igGrid("saveChanges", function (data) { window.location.href = " https://www.someotherurl.com " }, function(jqXHR, textStatus, errorThrown) { $("#message").text("An error occurred while saving the changes. Error details: " + textStatus).fadeIn(3000).fadeOut(5000); let container = $('#grid').igGrid('scrollContainer')[0] container.scrollTop = 0; });
Please let me know if you have some additional questions regarding this matter.