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

Popup editor: what to return when model validation fails?

1 Answer 368 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 14 Jan 2016, 01:29 AM

I have an MVC Razor app with the Kendo Grid using the popup editor modeled on the demo.  The popup comes up OK, and the model is invoked and does it's validation steps.  I do not use Entity Framework, the model has validation logic that applies business rules.  My problem is that when validation fails, or succeeds for that matter, the demo does not show what to return to the view/grid.  How are errors formatted for example? 

My other question is what to return if successful?  The object the controller got from the editor is a singular object, not IQueriable or IEnumerable.  My grid is populated with data, and the model has already done the insert to the database if validation passed.  Do I refresh the grid, and if so, how can I make sure the user is kept in the same section of the grid?

My controller code for Create is shown below (the return code shows an Intellisense error you can't see here):

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, BuyerAssignmentRecord bar)
{
    if (bar != null)
    {
        if (bar.DepartmentOI == 0) { bar.DepartmentOI = -1; }
        if (bar.EmployeeOI == 0) { bar.EmployeeOI = -1; }
 
        if (bar.Validate())
        {
            bar.Create();
        }
        else
        {
            // todo: what to return if validation failed?
        }
    }
    return Json(new[] { BuyerAssignmentRecord }.ToDataSourceResult(bar, ModelState));
}

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 15 Jan 2016, 02:29 PM

Hello Richard,

 

Straight to your questions: 

 

  1. When the validation fails I would suggest to add ModeState error as shown in the Handling Server-Side Validation Errors In Your Kendo UI Grid blog post. This is why the model state is return to the client-side. Of course the record should not be added to the data base. 

 

  2. It does not need to return a collection of items. You can return the newly created object as shown in the Ajax Editing article. 

 

Regards,
Boyan Dimitrov
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
Richard
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or