Validation with WebAPI

2 posts, 0 answers
  1. Matthijs
    Matthijs avatar
    3 posts
    Member since:
    Jun 2013

    Posted 21 Jan Link to this post

    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?

  2. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    347 posts

    Posted 25 Jan Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top