Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
960
How to return an error message form MVC controller GridDataSourceAction to the View, for display in the web page?
posted

Hi

igniteUI 13.1, MVC 4

I have an igGrid in my MVC view and it is populated via the DataGridModel.DataSourceURL. This calls a method in my controller to get the data, which takes the form of a strongly typed data table.

Under certain circumstances something can go wrong in the controller which prevents the data from being retrieved. When this happens I need to get a message back to the View stating the problem.

In this particular case the grid data is populated in part from an external website via an API which has a transactions per hour limit on it. When my system exceeds that limit I get no data data but I do get a message telling me how long I must wait before trying again. I want to get that message back to my view. eg "API call limit exceeded, try again after 35 minutes".

I have tried various things but I can't figure out how to get an error message back to the View from the GridDataSource action method in my controller. Here it is as it stands right now:

[GridDataSourceAction]
public ActionResult GetTicketGridData()
{
dsHelpdesk oTickets = new dsHelpdesk();
Exception oException = null;
try
{
oTickets = clsInvoiceHelper.GetTicketData();
}
catch (Exception ex)
{
oTickets = null;
oException = ex;
}// try
if (oTickets == null)
{
return View(ACTION_InvoicePrepGetData, oException.Message);
}
else
{
return View(ACTION_InvoicePrepGetData, oTickets);
}
}// GetTicketGridData

The above code results in the Load animation running forever and nothing comes back to my view. Under the hood a Object reference not set to an instance of an object exception is thrown, presumably because the datatable is not being returned from my controller method.

The best that I have come up with is to clear out the datatable and insert a new row with the error message in one of its string fields. This just looks awful on the web page.

If I could intercept this duff data row I could work around this but I can't find the right event eg a AfterDataLoaded event on the grid or  igDatasource.

Regards,

Graeme

  • 25665
    Offline posted

    Hello Graeme,

    Thank you for contacting Infragistics!

    The event that would be fired on a failed request would be the requestError event:

    Click the following link then go to the events tab: http://help.infragistics.com/jQuery/2014.1/ui.iggrid

    Currently the requestError event does not receive the server text/message that you return into your method. So you would have to find your own way to determine that was the cause then display the message. If you would like to see this as a feature in the grid you can make a new product idea:

    You can suggest new product ideas for future versions (or vote for existing ones) on our UserVoice website: http://ideas.infragistics.com/

    There are many benefits to submitting an idea through UserVoice:

    - Communicate directly with our product management team. They'll see the idea described in your words and respond to you personally. - Receive notifications whenever new information regarding your idea becomes available. - Vote on your favorite product ideas to let us know which ones are the most important to you. - You and other developers can discuss existing product ideas with members of our Product Management team.

    Thank you for your request.

    The other method to achieve this would be to have your own custom ajax request and return and display the message.

    Please let me know if you have any further questions concerning this matter.

  • 2745
    Verified Answer
    Offline posted

    You can manually do an AJAX request yourself instead of using the MVC Helper.  This way you get full control.

    For example:

    $.ajax({
      url: "{controller}/GetTicketGridData",
      type:  "POST",
      cache: false,
      success: function(data) {
        $("#selector").igGrid("option", "dataSource", data);
        $("#selector").igGrid("dataBind");
      },
      error: function (request, status, error) {
        $("#errorMessage").text(request.responseText);
      }
    });

    This way you can remove the [GridDataSourceAction] attribute from the method signature and just return Json();