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

Rebind grid after delete

8 Answers 155 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.
raphael
Top achievements
Rank 1
raphael asked on 02 Apr 2012, 10:38 AM

Hi I'm stucked with this problem.

When I try to rebind the grid after I deleted a record the grid won't be refreshed. I used the same rebinding code for the insert and the update and it works fine.

 

here is the code in the controller:

 

[AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Insert(UsersModel user)
        {
            if (ModelState.IsValid)
            {
                _usersRepository.Insert(user);
            }
              
            //Rebind the grid
            return View(new GridModel( _usersRepository.GetList(new Criterion("Firstname", "ASC"))));
        }
  
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(UsersModel user)
        {
            _usersRepository.Delete(user.UsersId);
           //Rebind the grid
           return View(new GridModel(_usersRepository.GetList(new Criterion("Firstname", "ASC"))));
        }
  
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Update(UsersModel user)
        {
            if (ModelState.IsValid)
            {
                _usersRepository.Update(user);
            }
              
            return View(new GridModel(_usersRepository.GetList(new Criterion("Firstname", "ASC"))));
        }

and this is the one in the cshtml

@(Html.Telerik().Grid(Model)
    .HtmlAttributes(new { style = "width: 550px" }) 
    .Name("Grid")
        .DataKeys(k => k.Add(r => r.UsersId).RouteKey("UsersId"))
        .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.BareImage).ImageHtmlAttributes(new { style = "margin-left:0" }))
    .Columns(columns =>
    {
        columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.BareImage);
                commands.Delete().ButtonType(GridButtonType.BareImage);
            }
               ).Width(75);
        columns.Bound(u => u.FirstName).Title("First name").Width(150);
        columns.Bound(u => u.LastName).Title("LastName name").Width(150);
        columns.Bound(u => u.LastActivityDate).Width(130).Format("{0:dd.MM.yyyy }");
         
    })
        .DataBinding(dataBinding => dataBinding.Ajax()
                        .Select("getUsers", "Users")
                        .Insert("Insert", "Users")
                        .Update("Update", "Users")
                        .Delete("Delete", "Users"))
    
    .Editable(editing => editing.Mode(GridEditMode.PopUp))
    .Sortable()
    .Scrollable()
)

can somebody say me what I'm doing wrong?

thank you very much

Raphael

8 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 02 Apr 2012, 01:32 PM
Careful you use server AND ajax binding:

@(Html.Telerik().Grid(Model) =>Server binding

.DataBinding(dataBinding => dataBinding.Ajax().Select("getUsers", "Users")=> Ajax binding

You don't need the .Grid(Model)

Replace it by .Grid<UsersModel>()

Try if this change your behavior 
0
raphael
Top achievements
Rank 1
answered on 02 Apr 2012, 01:41 PM
I did the change but I still have the same behavior. I just can't figure out why the problem appears only when I try to refresh the grid after I delete a record, but work after an insert and an update.
0
Dadv
Top achievements
Rank 1
answered on 02 Apr 2012, 01:50 PM
can we see your select method please?
0
raphael
Top achievements
Rank 1
answered on 02 Apr 2012, 01:57 PM

for the select method I just do this:

public List<Users> GetUsers(string sortExpression)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return context.Users.OrderBy(sortExpression).ToList();
            }
        }

and when i debug after deleting my test data, i get back the right result from the query.
0
Dadv
Top achievements
Rank 1
answered on 02 Apr 2012, 02:23 PM
http://demos.telerik.com/aspnet-mvc/grid/editingajax => this is a good start to go

I have seen this in what you copy paste :

.DataBinding(dataBinding => dataBinding.Ajax()
                        .Select("getUsers", "Users") // "G"etUsers (but this will not be the initial behavior

I think your select should look like this :

[GridAction] 
public ActionResult GetUsers()
        {           
return View(new GridModel(Data = _usersRepository.GetList(new Criterion("Firstname""ASC"))));
        }

and you can probably change delete by that :

[AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(int usersId)
        {
            _usersRepository.Delete(usersId);
           //Rebind the grid
return View(new GridModel(_usersRepository.GetList(new Criterion("Firstname""ASC"))));
      
        }

Give us a sample project if possible to see the behavior in action

0
raphael
Top achievements
Rank 1
answered on 03 Apr 2012, 06:43 AM
Hi I had to work a bit to put together a sample application to upload.

Instead of the user I kept the role part that has the same problem. I hope this can help to find the problem

thank you

raphael
0
Accepted
Dadv
Top achievements
Rank 1
answered on 03 Apr 2012, 02:59 PM
You use SOA approach, love it ^^

 I had already give you the solution :) replace "RolesModel role" by "int usersId" in the Delete method

public ActionResult Delete(int usersId)
        {
            _usersRepository.Delete(usersId);
           //Rebind the grid
return View(new GridModel(_usersRepository.GetList(new Criterion("Firstname""ASC"))));
        }

your problem was that the RolesModel have a [Required] on the Name property, so if you add it in the param list for the method, MVC will add a ModelState Error to the result. Telerik see this and try to add it to a Validation.. but none are in the page.. so nothing change in the page.

Now the method ask only RolesId, no requirement inside, so no ModelState add, Telerik recive a "clean" response, and refresh the grid.


Regards, 

PS: great project :)

0
raphael
Top achievements
Rank 1
answered on 04 Apr 2012, 06:50 AM
Oh sorry,

I did not notice that I had to change the parameter in the delete action the first time you gave me the solution!

Thank you very much for the explaination, now everything works.

I hope to get everything to work in the project, first time I use MVC and I'm not so used to it ;)

Thank for the support

Raphael
Tags
Grid
Asked by
raphael
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
raphael
Top achievements
Rank 1
Share this question
or