18 Answers, 1 is accepted
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.
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>
[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
Thanks,
-ldl-
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
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 });
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
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-
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
Thanks,
-ldl-
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.
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-
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
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.
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
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.
Atanas Korchev
the Telerik team
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