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

[Solved] Return a custom Validation Message

6 Answers 238 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.
Reid
Top achievements
Rank 2
Reid asked on 23 Jan 2012, 09:07 PM
Hi all,

Is it possible to return a custom message in the process of the built in Validation for the MVC Grid?  I mentioned today in a post, I am trying to implement the in-line Ajax Editing of records in a Grid using this as an example:

http://demos.telerik.com/aspnet-mvc/grid/editingajax

I have it working basically.  I am having a problem with TryUpdateModel() never considering the ViewModel valid even though it is.  But in the discovery process I decided to implement a Validator in the model, and if it fails call TryUpdateModel() and then return the same ViewModel.  That seems to fire the Telerik code for validation in that the fields that have their member adorned with the right attributes show up with a message.

So at this point I would like to do a duplicate check prior to the INSERT and if the record exists, not cancel the action but return and inform the user of the duplicate record.  I would like to use the grid to do it if possible.  Otherwise I could create a model member that would display a div with a message.

Thanks,
Reid

6 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 25 Jan 2012, 04:26 PM
Hi Reid,

You could use the controller's ModelState property to check what validation errors are found for the view model. The same property can be used to send custom validation messages to the client e.g.
ModelState.AddModelError("errorKey","errorMessage");
The messages can be get on the client by adding a handler to the Grid's OnError client-side event and inspecting the event argument's modelState property.
function onError(e) {
    if (e.textStatus == "modelstateerror") {
        e.modelState.errorKey.errors[0];
    }
}


All the best,
Daniel
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
Reid
Top achievements
Rank 2
answered on 25 Jan 2012, 09:02 PM
Hello Daniel,

Thanks for answering that.  I implemented it as you suggest and it works other than I do not see the message showing up anywhere.  Also I had to usee.modelState.modelstateerror.errors[0];

Any idea what might be happening with the meesage not showing?  I see it in the JS debugger.

Thanks again,

Reid
0
Daniel
Telerik team
answered on 26 Jan 2012, 12:43 PM
Hi Reid,

The key by which you can retrieve the error message is the same as the one used in the AddModelError method.
The messages for custom validation errors aren't shown automatically. You could use them to either alert the user or write them in a validation summary. An example about showing all model state errors is available in the Showing Model State Errors section in the OnError documentation.

Greetings,
Daniel
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
Reid
Top achievements
Rank 2
answered on 26 Jan 2012, 07:46 PM
Daniel,

Thank you for clarifying that.

Reid
0
David
Top achievements
Rank 1
answered on 29 Oct 2012, 04:22 PM
Under valid conditions, I am successfully returning the following to my view after a grid insert or update:
       
[GridAction(EnableCustomBinding = true)]
        private ActionResult SaveConcurrentReview(AdmissionConcurrentReview concurrentReview, GridCommand command)
        {
...
return Json(new GridModel
            {
                Data = concurrentReviewsModel,
                Total = concurrentReviews.Count(),
                Aggregates = aggregates
            });

This causes the grid to automatically update itself with the new values, and close the built in popup editing form.

I am trying to incorporate the OnError client event, and have tried the code suggested

[GridAction(EnableCustomBinding = true)]
        private ActionResult SaveConcurrentReview(AdmissionConcurrentReview concurrentReview, GridCommand command)
        {
...
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("myErrorKey", "my error message");
        return View("_admissionConcurrentReview", new GridModel { Data = null, Total = 0 });


It does indeed fire the OnError event, but not with a modelState error, but rather textStatus = 'parseerror'.  The view is basically empty, as I just want to inspect the model state and update the current built in grid dialog:

@model Telerik.Web.Mvc.GridModel
@{
    Layout = null;
}

Please advise...


                


0
Daniel
Telerik team
answered on 01 Nov 2012, 11:46 AM
Hello David,

The response from the server should not be a View. The GridActionAttribute should convert the ViewResult returned from the action to a JsonResult with the data. The problem occurs because null is specified for the Data of the GridModel. The GridActionAttribute always expects a collection and you should provide at least an empty list in order to avoid the problem.

Regards,
Daniel
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Grid
Asked by
Reid
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Reid
Top achievements
Rank 2
David
Top achievements
Rank 1
Share this question
or