Thursday, December 22, 2011

Telerik MVC Grid won't refresh after delete

For a project we are using the Telerik MVC controls. I have to say that I was really pleased with the experience in general, although I had some small issues.

One of the problems I had was with the editing feature of the Telerik MVC Grid. Everything appears to work as expected except for the delete. The delete MVC action is called and the row is  deleted from the database but the grid does not refresh.

[HttpPost]
[GridAction]
public ActionResult DeleteCodetable(string codetableType, Codetable codetable)
{
_codetabelService.Delete(codetable);
//Rebind the grid
return LoadCodetable(codetableType, refreshCache: true);
}


It took me some time to find the answer on the forums. The delete action only passes the id of the element to delete to the action method. This causes some model binding validation errors as the Code and Description field are required on my Codetable object.



public class Codetable
{
[Required]
public virtual int Id { get; set; }
[Required]
public virtual string Code { get; set; }
[Required]
public virtual string Description{ get; set; }
}


By design if there are model state errors (validation didn't work) the grid is not rebound. So when the DeleteCodetable method returns the result, the grid will not refresh. I solved it by introducing the IgnoreModelErrors attribute in my code.



[HttpPost]
[IgnoreModelErrors("Code,Description"]
[GridAction]
public ActionResult DeleteCodetable(string codetableType, Codetable codetable)
{
_codetabelService.Delete(codetable);
//Rebind the grid
return LoadCodetable(codetableType, refreshCache: true);
}

No comments: