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

Validation with WebAPI

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthijs
Top achievements
Rank 1
Matthijs asked on 21 Jan 2016, 12:47 PM

Hi,

Q: Is server validation possible with WebAPI as DataSource and Kendo UI grid?

Situation: First, the model for the gid contains an attributed field "required". But the grid doesn't do any validation for this field. The Web API POST request is happening when this field still empty (even the validation warning isn't displaying (DisplayMessage)).

Then, inside the WebAPI request at the server-side, the returned object is 'HttpResponseMessage'. So how can i trigger the grid that the validation is false (ModeState.IsValid == false). 

 I known the exiting of a returned object DataSourceResult with second parameter 'ModelStateDictionary', but this expect the ModelStateDirectory of a MVC controller and not from an API Controller.

So the goal is, how can I do a validation on grid inline edition submit with Web API?

1 Answer, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 25 Jan 2016, 12:28 PM
Hello Matthijs,

You can examine the following demo for Kendo UI Grid inline editing:

http://demos.telerik.com/aspnet-mvc/grid/editing-inline

If you try to enter an empty ProductName, an error message will be displayed.

The important points, regarding validation, are:

- Set the actions and the error handler to be called upon interacting with the Grid
C#:

.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.ProductID))
.Create(update => update.Action("EditingInline_Create", "Grid"))
.Read(read => read.Action("EditingInline_Read", "Grid"))
.Update(update => update.Action("EditingInline_Update", "Grid"))
.Destroy(update => update.Action("EditingInline_Destroy", "Grid"))

- create the error handler JavaScript function
JS:

function error_handler(e) {   
    if (e.errors) {
        var message = "Errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function() {
                    message += this + "\n";
                });
            }
        });       
        alert(message);
    }
}

- check the ModelState, based on the validation attributes, set in the Model class
C# Controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
{
    if (product != null && ModelState.IsValid)
    {
        productService.Update(product);
    }           
 
    return Json(new[]{product}.ToDataSourceResult(request,ModelState));
}

I hope this helps, but if it doesn't, please send us a runnable code example, demonstrating the issue, so we can investigate the issue.

Regards,
Dimiter Topalov
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Matthijs
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Share this question
or