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

Control delete/edit errors in ajax mode

18 Answers 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joan Vilariño
Top achievements
Rank 2
Joan Vilariño asked on 07 Jun 2010, 08:22 AM
Hello.

Is there any way to pass back a message from a GridAction in Ajax mode?

I have a "Delete" button in my rows, but sometimes due to business rule I must deny the deleting of the record. Is it possible to return a "can't delete this record" message back to the grid in any way from the GridAction method?

Thanks!

18 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 07 Jun 2010, 08:52 AM
Hello Joan VilariƱo,

Unfortunately this is not currently possible. We plan to add ability to serialize the model state in the future in order to handle scenarios like this. However for the time being I cannot propose a workaround apart from using server binding/editing.

Regards,
Atanas Korchev
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
Joan Vilariño
Top achievements
Rank 2
answered on 15 Sep 2010, 09:05 AM
I've found a solution for this issue, it's a little clunky but it works for what I need.

I you bind the clientevent "onError" to a javascript function, you can catch any exception on serverside, like this:

<%= Html.Telerik().Grid<MyModel>()
           ........
           ........
           ........
           .ClientEvents(ev => ev.OnError("onMyGridError")
           ........
           ........
%>
  
<script type="text/javascript">
  
     function onMyGridError(e) {
        e.preventDefault();
        var errorText = new RegExp('<title>(*.)</title>').exec(e.XMLHttpRequest.responseText)[1];
        alert('Error: '+errorText);
     }
  
</script>

At controller's side you would do the following:

[GridAction]
public ActionResult _myGridUpdate(long id) {
  if (ModelState.isValid()) {
     // bleh bleh bleh...
     return View(new GridModel<MyModel> { Data = xxxxxxxxxxx });
  }
  else {
     throw new Exception('This is the message text... maybe you could even include XML?');
  }
}

I hope this can help anyone having this same issue.

Cheers
0
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 29 Oct 2010, 10:27 PM
This is a rather ugly situation.  I was disappointed to see that the latest beta does not serialize the model state in this case (at least it is not in the release notes).  Hopefully this will go in soon?

Thanks,

-ldl-
0
Ryan
Top achievements
Rank 1
answered on 09 Nov 2010, 03:42 PM
I'm also very disappointed to not have the ability to send the modelstate back to the view.  I'm looking to move one of my large projects (using jqgrid) over to Telerik, but much of the validation requires a heavy amount of server processing and isn't feasible client-side.  I love the direction it's headed, but until this is available the plugin is more or less useless for me.
0
Atanas Korchev
Telerik team
answered on 09 Nov 2010, 04:12 PM
Hello Ryan,

 Actually this is possible since the service pack release. ModelState errors are serialized to the client-side and the grid displays them automatically.

Regards,
Atanas Korchev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Ryan
Top achievements
Rank 1
answered on 09 Nov 2010, 04:35 PM
This is great news! thank you so much.
0
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 01 Dec 2010, 12:37 AM
I am using the Q3 release.  I expected the code below to populate my view's  ValidationSummary.  Am I missing a concept somewhere?

if (sellKeysRegion != null)
{
    db.SellKeysRegions.DeleteObject(sellKeysRegion);
    try
    {
        db.SaveChanges();
    }
    catch (Exception)
    {
        ModelState.AddModelError("", "Could not delete this record - it is in use (Foreign Key).");
    }
}
 
IQueryable<SellKeysRegionModel> model = GetModel();
 
return View("Index", new GridModel { Data = model });

0
Atanas Korchev
Telerik team
answered on 01 Dec 2010, 07:58 AM
Hello Larry,

 The grid can display errors that are associated with some member of the model. You are using empty string when adding the model state error and this is the problem. Try to specify the name of some property there.

Regards,
Atanas Korchev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 01 Dec 2010, 09:23 PM
Thanks Atanas.  I assumed (though I didn't verify) that attaching the model state error to the grid itself would work.  The specific error I am trapping isn't tied to the grid though, which is why I wanted to display it in a ValidationSummary control.

So are you folks are using some private mechanism to serialize the model state versus the normal  mechanism that MVC itself uses?

I have a workaround so this is not a blocker for me.  But I would have expected my code to work (showing only model level errors) so you might want to give it some thought...

Thanks!

-ldl-
0
Atanas Korchev
Telerik team
answered on 02 Dec 2010, 08:39 AM
Hi Larry,

 It is the code which displays the errors which needs the model member name. We display validation errors next to the associated edit control and that's why we need the name. We don't know where to display an error which is not associated with a member. 

Regards,
Atanas Korchev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 02 Dec 2010, 07:30 PM
My point is that I do not want the grid to display a message.  I want the model state populated so that I can display a 'cannot delete this record' message in a ValidationSummary control.  This is what the 'model level' (no property name) AddModelError syntax is for.  Hope that makes sense.

Thanks,

-ldl-
0
Ryan
Top achievements
Rank 1
answered on 02 Dec 2010, 08:48 PM
Larry,

I don't believe you'll be able to use the ValidationSummary helper with this grid in ajax mode.  That helper is executed on the server and turns into an HTML list.  When in ajax mode, this helper will only be executed when the page is first rendered.
0
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 02 Dec 2010, 09:02 PM
That makes sense.  I thought I might be missing something.

However, in playing around with it I also got stumped in what grid property name I could use in the AddModelError call to get the message to show up.  I tried a number of things, none of which worked.

Can I get an example of what this would look like?  That is, I try to update the model, get an exception, and want to use AddModelError to create a message, then I want that message displayed in the grid.  Can you show me a small working example of that?

Thanks!

-ldl-
0
Atanas Korchev
Telerik team
answered on 03 Dec 2010, 08:44 AM
Hi Larry,

 Find attached a sample project showing how to use model state errors. Check HomeController.cs for the actual implementation.

Regards,
Atanas Korchev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Joao Carlos
Top achievements
Rank 1
answered on 09 Dec 2010, 04:20 AM
I have a similar problem. I need to show an error message about the DELETE action.
When deleting, even if I add a ModelStateError, it's not shown, because the Fields on the grid are NOT in edit mode.

How can I show a "Unable to delete this record" message?

Thanks in advance.
0
Victor
Top achievements
Rank 1
answered on 11 Jan 2011, 02:41 PM
Hi!

We are using the latest version and after looking at your attached project, that looks nice. However, adding a _Delete method and copying the content in _Update no model error is shown. The ModelState is indeed sent to the client in the same way, but there seems to be no client code to take care of it. Apart from the built-in error handling, it would be very nice with a possibility to take care of the errors in a custom way using the api (with the possibility to cancel the built-in behaviour).

Finally I agree with Joao that it would be very nice to be able to handle generic errors (for me one api event each for edit/delete error handling would be perfect, preferably with both an array of individual errors as well as the server-side validation summary)

Is there any plan to resolve these problems / add this functionality?

Edit: There is also another problem (bug?) - if the column which is causing the error is set to read only the error is never displayed, the update operation just silently fails.

Thanks
/Victor
0
Atanas Korchev
Telerik team
answered on 11 Jan 2011, 03:39 PM
Hi,

We have to think of some way to expose the errors to the end user via the existing OnError event or a new one. Still I cannot commit with a deadline when this is going to be implemented as the tasks for the next major release are already determined and it is too late to change priorities.

We do not display errors for readonly columns because the latter lack validator html elements (spans). We use those spans to show the errors as ASP.NET MVC does server side.

Regards,
Atanas Korchev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Victor
Top achievements
Rank 1
answered on 11 Jan 2011, 04:42 PM
Ok, thank you for that. We will look into if it worth to implement a custom solution based on the httprequest or not. Thanks for letting us know though.

For the record we would prefer a separate API method since a model error is very different from a channel error. And about the readonly part it seems like a slightly strange reason. Possible the best solution would be to add those spans? I realize that you follow what DisplayForModel ususally does, but since that is for read ONLY of the full object I would argue that the situation is a bit different.

Thanks for the quick reply
/Victor
Tags
Grid
Asked by
Joan Vilariño
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Joan Vilariño
Top achievements
Rank 2
Larry
Top achievements
Rank 1
Iron
Iron
Veteran
Ryan
Top achievements
Rank 1
Joao Carlos
Top achievements
Rank 1
Victor
Top achievements
Rank 1
Share this question
or