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

Ajax binding and AddModelError

1 Answer 140 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.
Greg Lynne
Top achievements
Rank 1
Greg Lynne asked on 10 May 2012, 03:40 AM
Hi Telerik,

I was hoping that you might be able to confirm that the following should work or not on the initial binding?

Should the OnError event get thrown when you apply ModelState.AddModelError during the initial bind of the grid via ajax, or do I need to set Response.StatusCode = 500 everytime to create the error but this will not create a modelstateerror

I am using version 2012.1.214

Please see the follwing code below:

VIEW
@{
    Html.Telerik().Grid<TaskDto>()
        .Name("Tasks")
        .ClientEvents(events => { events.OnError("Grid_onError"); }           
        )
        .Columns(columns =>
        {              
            columns.Bound(o => o.Title).Title("Title");          
        })      
        .Sortable()
        .DataBinding(
            dataBinding => dataBinding.Ajax()
            .Select("_Tasks", "Task")       
        )
        .EnableCustomBinding(true)
        .Render();
}


Javascript Handler
function Grid_onError(args) {
    alert(args.textStatus);
 
    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);
    }
}

Controller
           [HttpPost]      
        [GridAction(EnableCustomBinding = true)]
        public ActionResult _Tasks(GridCommand command)
        {
               try
               {                  
                   return View(new GridModel<TaskDto>() { Data = _proxy.CurrentTasks() });
               }
               catch(Exception exception)
               {
                  // Response.StatusCode = 500;
 
                   ModelState.AddModelError("Error", "Proxy error.");
                   return View(new GridModel<TaskDto>() { Data = new List<TaskDto>() {  } });
                   
               }
           }           
        }

1 Answer, 1 is accepted

Sort by
0
Greg Lynne
Top achievements
Rank 1
answered on 10 May 2012, 03:54 AM
Or as an alternative to setting the Response.StatusCode = 500; should I subscripe to the OnComplete event as mentioned in the following thread http://www.telerik.com/community/forums/aspnet-mvc/grid/handle-ajax-delete-error.aspx

PS: I did look really hard through your forum to find an answer for this but was unsure of the correct approach.
Tags
Grid
Asked by
Greg Lynne
Top achievements
Rank 1
Answers by
Greg Lynne
Top achievements
Rank 1
Share this question
or