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

[Solved] How to return error information to grid in ajax editing mode

5 Answers 453 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.
Jao
Top achievements
Rank 1
Jao asked on 20 Jul 2010, 08:23 AM
Hi!
Is there a way to tell the user who currently inserting or editing grid row the that some special kind of data error occured. For instance attempt to create duplicate record or attempt to violate any other database logic. GridModel allows me to specify updated data only, not error code or message. And second. I would like to use some complex validation logic with localized validation error messages when processing CRUD ajax requests. How can I achieve that?
Example code:
var entity = new Entity();
if (TryUpdateModel(entity))
{
Repository.Entities.InsertOnSubmit(entity);
Repository.SubmitChanges();
} // need else branch to display model state errors to user
return View(new GridModel(Repository.Entities));
Thank you.

5 Answers, 1 is accepted

Sort by
0
Jao
Top achievements
Rank 1
answered on 20 Jul 2010, 09:46 AM
// Controller
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _InsertEntityt()
{
var entity = new Entity();
if (TryUpdateModel(entity))
{
Repository.Entities.InsertOnSubmit(entity);
Repository.SubmitChanges();
}
else
{
Response.StatusCode = 500;
                return Content(String.Join(
                    " ",
                    (from state in ModelState select state)
                        .SelectMany(s => s.Value.Errors)
                        .Select(e => e.ErrorMessage)
                        .ToArray()));
}
return View(new GridModel(Repository.Entities));
} // View <%=
	Html.Telerik().(...).ClientEvents(events => events.OnError("onError"))
%>
<script type="text/javascript">
    function onError(e) {
        if (e.textStatus == 'error'){
            if (e.XMLHttpRequest.status == "500") {
                alert("The server was unable to process your request.\n" + e.XMLHttpRequest.responseText);
                e.preventDefault();
            }
        }
    }
</script>
0
Jao
Top achievements
Rank 1
answered on 20 Jul 2010, 09:49 AM
I'm sure all the logic above should be incapsulated into HandleGridError filter attribute.
0
Saul
Top achievements
Rank 1
answered on 22 Jul 2011, 03:54 PM
How would you do this if IIS is set to use the default error pages? When I deploy this solution I do not get the error message rather the text from the default error page.

Thanks

0
Xavi
Top achievements
Rank 1
answered on 07 Sep 2011, 12:10 PM
You are the man.
Thanks for sharing
0
Mano
Top achievements
Rank 1
answered on 05 Jun 2013, 09:42 PM
But how do you handle for Successful Adding/Editing of the record. Can somebody please point out a direction to look into? 
Tags
Grid
Asked by
Jao
Top achievements
Rank 1
Answers by
Jao
Top achievements
Rank 1
Saul
Top achievements
Rank 1
Xavi
Top achievements
Rank 1
Mano
Top achievements
Rank 1
Share this question
or