This question is locked. New answers and comments are not allowed.
Scenario:
Ajax Grid with In-Line Editing of SomeTable [Id (int PK), Description (varchar(50)), Cost (money)]
Entity Framework and a Service/Repository pattern is used.
The Description has a unique constraint which will not allow duplicate Descriptions on the table. If a Duplicate entry is attempted to be created then, than an SQL Exception is thrown.
Goal: Try to catch errors (Exceptions) that may occur on the server side (at the Repository, EF, or SQL Level) that may occur.
The Grid Shows columns for Description and Cost and has the following Data Binding:
CONTROLLER ACTION _Insert called by Ajax Grid to Insert a Record
The Controller instantiates _service when the controller is instantiated
_service = new Service(new ModelStateWrapper(ModelState));
_INSERT ACTION CODE:
After the _Service.InsertSomeTable(Item) where an entry already exists for the table entry:
The Grid will stay in the "Edit" Mode, but no error messages are shown. What I was expecting to see was a "Validation Summary" that showed all of the Model Errors that were added with a blank key (string.empty);
How can this be made to work with an Ajax Editable Grid such that some sort of "Validation Summary" is shown that will
show the errors in Model State that do not have a corresponding key?
I was able to make this work with dataBinding.Server() by placing the following in the View:
<%: Html.ValidationSummary(true, "Errors have occurred! Please try again.") %>
But it should work that way.
Thanks in Advance
Ajax Grid with In-Line Editing of SomeTable [Id (int PK), Description (varchar(50)), Cost (money)]
Entity Framework and a Service/Repository pattern is used.
The Description has a unique constraint which will not allow duplicate Descriptions on the table. If a Duplicate entry is attempted to be created then, than an SQL Exception is thrown.
Goal: Try to catch errors (Exceptions) that may occur on the server side (at the Repository, EF, or SQL Level) that may occur.
The Grid Shows columns for Description and Cost and has the following Data Binding:
dataBinding.Ajax()
.Select("_Index", "SomeTable")
.Insert("_Insert", "SomeTable")
.Update("_Update", "SomeTable")
.Delete("_Delete", "SomeTable");
CONTROLLER ACTION _Insert called by Ajax Grid to Insert a Record
The Controller instantiates _service when the controller is instantiated
_service = new Service(new ModelStateWrapper(ModelState));
_INSERT ACTION CODE:
[HttpPost]
[GridAction]
public ActionResult _Insert()
{
//Create a new instance of the item class.
SomeTable item = new SomeTable();
//Perform model binding (fill the item properties and validate it).
if (TryUpdateModel(item))
{
//The model is valid - insert the item.
_service.InsertSomeTable(item);
// If any errors occur - ModelState.Isvalid will be false and the errors
// will be in the ViewData.ModelState.Values
//
// For an exception such as an SQL Error (Duplicate values on Unique Constraint)
// the exception Message will have been added into the values by the service
// where the key is empty.
// ie. modelState.AddModelError(string.empty, ex.Message);
}
// Rebind the grid
return View(new GridModel(_service.GetCompanyDirectoryCategories()));
}
After the _Service.InsertSomeTable(Item) where an entry already exists for the table entry:
- The ModelState is not valid (!ModelState.IsValid) but all of the Model Errors have a blank (sting.empty) key.
The Grid will stay in the "Edit" Mode, but no error messages are shown. What I was expecting to see was a "Validation Summary" that showed all of the Model Errors that were added with a blank key (string.empty);
How can this be made to work with an Ajax Editable Grid such that some sort of "Validation Summary" is shown that will
show the errors in Model State that do not have a corresponding key?
I was able to make this work with dataBinding.Server() by placing the following in the View:
<%: Html.ValidationSummary(true, "Errors have occurred! Please try again.") %>
But it should work that way.
Thanks in Advance
13 Answers, 1 is accepted
0

Bese
Top achievements
Rank 1
answered on 08 Jan 2011, 12:15 PM
I've the same question. If you've any solution, please tell us.
0

Steve
Top achievements
Rank 1
answered on 18 Mar 2011, 07:27 AM
Still have not heard anything on this solution.
0

Radu
Top achievements
Rank 1
answered on 17 Aug 2011, 07:44 PM
Hi Telerik
I am also VERY interested in getting an answer to this question.
How do you show Business or System errors/messages when AJAX binding the grid?
The only threads I've found deal with attribute validation only.
I really hope someone at Telerik can at least answer with a "Yay" or "Nay" on this topic.
Thanks in advance!
I am also VERY interested in getting an answer to this question.
How do you show Business or System errors/messages when AJAX binding the grid?
The only threads I've found deal with attribute validation only.
I really hope someone at Telerik can at least answer with a "Yay" or "Nay" on this topic.
Thanks in advance!
0
Hi,
Atanas Korchev
the Telerik team
The grid does not support external validation summaries because in order for the latter to work it must be part of the form which is being validated. The grid is rendering this form by itself.
The recommended way of handling exceptions or server validation errors is via the OnError event. You can check this example which shows a sample implementation:
function Grid_onError(args) {Regards,
if (args.textStatus == "modelstateerror" && args.modelState) {
var message = "Errors:\n";
$.each(args.modelState, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function() {
message += this + "\n";
});
}
});
args.preventDefault();
alert(message);
}
}
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

Lyle
Top achievements
Rank 1
answered on 21 Aug 2011, 05:55 AM
Atanas... just so you know, your answers are always appreciated!
0

Radu
Top achievements
Rank 1
answered on 21 Aug 2011, 09:31 PM
Indeed, as Lyle says, the help is extremely appreciated!!
One last bit of help if you may: could you show what the controllers return statement might look like when returning to the grid's OnError event?
To be a little more precice, in the code you've posted, the args parameter has a textStatus and modelState attributes. How are these set on the controller action?
Thanks
One last bit of help if you may: could you show what the controllers return statement might look like when returning to the grid's OnError event?
To be a little more precice, in the code you've posted, the args parameter has a textStatus and modelState attributes. How are these set on the controller action?
function Grid_onError(args) {
if (args.textStatus == "modelstateerror" && args.modelState) {
Thanks
0

Steve
Top achievements
Rank 1
answered on 21 Aug 2011, 10:03 PM
Hi,
I tried this solution and it works. To add errors in the controller, you need to use:
The returned ActionResult for me is the same that when I initially bound my grid.
Hope this help
Steve
I tried this solution and it works. To add errors in the controller, you need to use:
this.ModelState.AddModelError("FieldX", "Error message");
The returned ActionResult for me is the same that when I initially bound my grid.
return View(new GridModel(listOfValuesToShow));
Hope this help
Steve
0

Radu
Top achievements
Rank 1
answered on 23 Aug 2011, 03:11 PM
Yes, this works perfectly (as counterintuitive as it may seem). Thanks to you all!
0

Serg
Top achievements
Rank 1
answered on 21 Oct 2011, 11:38 AM
Will this work with AjaxEditing (instead of incell). We could not make it work - the grid just updates, ignoring ModelState.
0
Hi Serg,
Rosen
the Telerik team
Indeed, this should work as explained by my colleague Atanas. Maybe you can send us a sample in which the described issue can be observed.
All the best,Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0

Serg
Top achievements
Rank 1
answered on 24 Oct 2011, 08:16 AM
It should but it does not.
I use ModelState.AddModelError in controller, followed by return Json(new GridModel(datahere)).
Then on client I use OnError handler, which does not even get called.
I will try to reproduce it once more and then open support ticket if there's a trouble.
(for now we just use our own client validation)
PS Which version of dll's should we have for that? I will double check if I have correct ones.
I use ModelState.AddModelError in controller, followed by return Json(new GridModel(datahere)).
Then on client I use OnError handler, which does not even get called.
I will try to reproduce it once more and then open support ticket if there's a trouble.
(for now we just use our own client validation)
PS Which version of dll's should we have for that? I will double check if I have correct ones.
0
Hi Serg,
You should use View method instead of Json in order ModelState to be serialized to the client.
All the best,
Rosen
the Telerik team
You should use View method instead of Json in order ModelState to be serialized to the client.
All the best,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0

mahmoud
Top achievements
Rank 1
answered on 30 May 2012, 10:02 AM
Thankssss!