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

[Solved] Can Column Custom Command send GridCommand and refresh grid?

1 Answer 87 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.
Andre
Top achievements
Rank 1
Andre asked on 29 Jun 2012, 03:17 PM
UPDATE:
When I went back to it, the binding is all working now. The only thing left is returning the result back to the view.

ORIGINAL QUESTION:
I believe that SendState should some how get the following CustomCommand to send a GridCommand to my Action, but binding does not seem to sort it out. Am I doing something wrong?

Also, in the examples I saw, the Action returns a JSONResult. Is it possible to return a GridModel so that the view will rerender it?

// This is the Custom Column Command I am trying to wire up
columns.Bound(v => v.IsDeleted).Title("Deleted").Width(100);
columns.Command(commands =>
{
   commands.Custom("delete")
      .Text("Delete")
      .SendState(true)
      .DataRouteValues(route => route.Add(v => v.Id).RouteKey("id"))
      .Ajax(true)
      .Action("Delete", "Victim")
      .ButtonType(GridButtonType.Image);
}
 
// This is the Controller Action that should consume the call
public ActionResult Delete(int id, GridCommand command)
{
   // delete the item
   return _Index(command);
}
 
// This is my working ajax Action which I want to recall to return a refreshed view
[GridAction(EnableCustomBinding = true)]
public ActionResult _Index(GridCommand command)
{
   var request = new GetPagedListItemsRequest
   {
      User = System.Web.HttpContext.Current.User,
      Command = command
   };
    
   var getPagedListItemsResponse = _myService.GetPagedListItems(request);
   var data = getPagedListItemsResponse.PagedList;
 
   return View(new GridModel { Data = data, Total = getPagedListItemsResponse.Count });
}

1 Answer, 1 is accepted

Sort by
0
Andre
Top achievements
Rank 1
answered on 29 Jun 2012, 04:48 PM
Well, I don't know whether you can return a GridModel from the Action, but returing a JSON result and then using the client onComplete event to bind the new data has achieved my requirement.

// The action method now returns a JSONResult with the current page's data
public ActionResult Delete(int id, GridCommand command)
{
   var request = new DeleteRequest
   {
      Id = id,
      User = System.Web.HttpContext.Current.User
   };
   var response = _myService.Delete(request);
 
   var refreshedModel = GetPagedItems(command);
            
   return new JsonResult
   {
      Data = new
      {
         items = refreshedModel.Data,
         total = refreshedModel.Total
      
   };
}
 
// refactored this out of the original _Index ajax method
private GridModel GetPagedItems(GridCommand command)
{
   var request = new GetPagedListItemsRequest
   {
      User = System.Web.HttpContext.Current.User,
      Command = command
   };
 
   var getPagedListItemsResponse = _myService.GetPagedListItems(request);
 
   var data = getPagedListItemsResponse.PagedList;
 
   return new GridModel { Data = data, Total = getPagedListItemsResponse.Count };
}
// this event will fire on all compeleted, and I will rebind when the name matches my custom event's name
function onComplete(e) {
 
   if (e.name == "delete") {
      var grid = $("#Grid").data("tGrid");
      grid.dataBind(e.response.items);
   }
}
// And to wire up the event, just add this to the Grid
.ClientEvents(events => events.OnComplete("onComplete"))
Tags
Grid
Asked by
Andre
Top achievements
Rank 1
Answers by
Andre
Top achievements
Rank 1
Share this question
or