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

[Solved] Telerik MVC Grid is not refreshing after deleting record using Ajax editing

2 Answers 314 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.
annu
Top achievements
Rank 1
annu asked on 14 Nov 2011, 06:15 AM
Hi,

I am trying to delete the record in Telerik MVC grid. But the grid is not refreshing after deleting the record. If I click the refresh button or press Ctrl+ F5 the grid is refreshed. I am using Ajax binding. It works fine after adding and editing the record (i.e the grid is refreshing).
Below is the code for Controller and View that I am using :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using TelerikMVC.Models;
using DAL;
using System.Data;
using System.Threading;
 
namespace TelerikMVC.Controllers
{
    public class RestaurantAJAXController : Controller
    {
        public ActionResult Index()
        {

            return View();
        }
 
        [GridAction]
        public ActionResult _AjaxBinding()
        {
            return View("Index", new GridModel(GetRestaurantData()));
        }
 
        private IEnumerable<RestaurantModels> GetRestaurantData()
        {
            POCDBContext db = new POCDBContext();
            IEnumerable<RestaurantModels> re = from r in db.Restaurants
                                               join ra in db.Restaurant_Address
                                                   on r.ID equals ra.ID
                                               select new RestaurantModels { ID = r.ID, Name = r.Name, City = ra.City, Country = ra.Country, EstablishedDate = r.EstablishedDate.Value };
 
            return re;
        }
 
        [HttpPost]
        [GridAction]
        public ActionResult Add(RestaurantModels rm)
        {
            POCDBContext db = new POCDBContext();
            Restaurant re = new Restaurant();
            re.Name = rm.Name;
            re.EstablishedDate = rm.EstablishedDate;
 
            db.Restaurants.AddObject(re);
            db.ObjectStateManager.ChangeObjectState(re, EntityState.Added);
 
            Restaurant_Address readd = new Restaurant_Address();
            //readd.ID = rm.ID;
            readd.City = rm.City;
            readd.Country = rm.Country;
            db.Restaurant_Address.AddObject(readd);
            db.ObjectStateManager.ChangeObjectState(readd, EntityState.Added);
 
            db.SaveChanges();
 
            return View(new GridModel(GetRestaurantData()));
        }
 
        [HttpPost]
        [GridAction]
        public ActionResult Update(RestaurantModels rm)
        {
            POCDBContext db = new POCDBContext();
            Restaurant re = new Restaurant();
            re.ID = rm.ID;
            re.Name = rm.Name;
            re.EstablishedDate = rm.EstablishedDate;
            db.Restaurants.Attach(re);
            db.ObjectStateManager.ChangeObjectState(re, EntityState.Modified);
 
            Restaurant_Address readd = new Restaurant_Address();
            readd.ID = rm.ID;
            readd.City = rm.City;
            readd.Country = rm.Country;
            db.Restaurant_Address.Attach(readd);
            db.ObjectStateManager.ChangeObjectState(readd, EntityState.Modified);
 
            db.SaveChanges();
            
            return View("Index", new GridModel(GetRestaurantData()));
        }
 
        public ActionResult Update()
        {
            return View();
        }
 
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(RestaurantModels rm)
        {
            try
            {
                ViewData["AJAXData"] = "";
                POCDBContext db = new POCDBContext();
                Restaurant re = new Restaurant();
                re.ID = rm.ID;
                //re.Name = rm.Name;
                db.Restaurants.Attach(re);
                db.ObjectStateManager.ChangeObjectState(re, EntityState.Deleted);
 
                Restaurant_Address readd = new Restaurant_Address();
                readd.ID = rm.ID;
                //readd.City = rm.City;
                //readd.Country = rm.Country;
                db.Restaurant_Address.Attach(readd);
                db.ObjectStateManager.ChangeObjectState(readd, EntityState.Deleted);
                db.SaveChanges();
                //Thread.Sleep(10000);
            }
            catch (Exception ex)
            {
 
 
            }
 
            return View(new GridModel(GetRestaurantData()));
 
            //  return View("Index");
        }
 
 
    }
}
The View is as follows:

@model IEnumerable<DAL.Restaurant>
@using Telerik.Web.Mvc.UI;
@{
    ViewBag.Title = "Index";
}
 
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<link href="../../Content/telerik.common.min.css" rel="stylesheet" type="text/css" />
<link href="../../Content/telerik.telerik.min.css" rel="stylesheet" type="text/css" />
<link href="../../Content/telerik.vista.min.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/telerik.grid.min.js" type="text/javascript"></script>
<script src="../../Scripts/telerik.grid.editing.min.js" type="text/javascript"></script>
@*<script src="../../Scripts/telerik.grid.editing.js" type="text/javascript"></script>*@
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
 
 
@(Html.Telerik().Grid<TelerikMVC.Models.RestaurantModels>()  //("AJAXData")
        .Name("Grid")
        .ToolBar(commands => commands.Insert())
        .DataKeys(dataKeys => dataKeys.Add(c => c.ID))
        
 
            .Columns(columns =>
            {
                columns.Bound(o => o.Name);
                columns.Bound(o => o.City).Width(100);
                columns.Bound(o => o.Country);
                columns.Bound(o => o.EstablishedDate).Width(170).Format("{0:dd/MM/yyyy}").Filterable(true);
                columns.Command(commands =>
                {
                     
                    commands.Edit().ButtonType(GridButtonType.BareImage);
                 
                    commands.Delete().ButtonType(GridButtonType.BareImage);
                     
                }).Width(200).Title("Actions");
                ;
 
            })
            .DataBinding(dataBinding => dataBinding.Ajax()            
                
                .Select("_AjaxBinding", "RestaurantAJAX")
                .Insert("Add", "RestaurantAJAX")
                .Update("Update","RestaurantAJAX")
                .Delete("Delete", "RestaurantAJAX")
                 
                )              
 
 
            .Editable(editing =>
                  {
                      editing.Window(win =>
                      {
                          win.Name("TextEdit");
                          win.Height(300);
                          win.Width(300);
                      });
                      editing.Enabled(true);
                      editing.DisplayDeleteConfirmation(false);              
                      editing.Mode(GridEditMode.PopUp);
                  })
 
 
            .Pageable(pager =>
            {
                pager.PageSize(5);
                pager.PageTo(1);
            })
        .Sortable()
        .Scrollable()
       
        .Filterable()
            
    )
 
@*For enabling the client side script*@
 
@(Html.Telerik().ScriptRegistrar())
 
Please check it and let me know where I am missing.

2 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 14 Nov 2011, 11:38 AM
Are you sure your EF code is working here as I cannot see any delete object call??

I would check in the DB to ensure the row is actually being deleted.  When using a stub and attaching it I would use syntax similar to this. Rather than ObjectStateManager.ChangeObjectState

var category = (from c in ctx.Categories
                where c.ID == 3
                select c).First();
 
// Delete the item
ctx.DeleteObject(category);
// Save changes
ctx.SaveChanges();

Alex James' Microsoft Blog explains in more detail

tips on deleting

Hope this helps...
0
Wahaj
Top achievements
Rank 1
answered on 21 Feb 2012, 07:57 AM
Tags
Grid
Asked by
annu
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Wahaj
Top achievements
Rank 1
Share this question
or