Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Here is the JQGrid setup information
C#
jQuery(document).ready(function () {
        jQuery("#list").jqGrid({
            url: '/TabMaster/GetGridData',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['col ID', 'First Name', 'Last Name'],
            colModel: [
                  { name: 'colID', index: 'colID', width: 100, align: 'left' },
                  { name: 'FirstName', index: 'FirstName', width: 150, align: 'left', editable: true },
                  { name: 'LastName', index: 'LastName', width: 300, align: 'left', editable: true },
                ],
            pager: jQuery('#pager'),
            rowNum: 4,
            rowList: [1, 2, 4, 5, 10],
            sortname: 'colID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: true,
            imgpath: '/scripts/themes/steel/images',
            caption: 'Tab Master Information'
        }).navGrid('#pager', { edit: true, add: true, del: true },
        //Edit Options
        {
        savekey: [true, 13],
        reloadAfterSubmit: true,
        jqModal: false,
        closeOnEscape: true,
        closeAfterEdit: true,
        url: "/TabMaster/Edit/",
        afterSubmit: function (response, postdata) {
            if (response.responseText == "Success") {
                jQuery("#success").show();
                jQuery("#success").html("Company successfully updated");
                jQuery("#success").fadeOut(6000);
                return [true, response.responseText]
            }
            else {
                return [false, response.responseText]
            }
        }
    });
});

following is the details for Getting Data and Update Data using JQGrid
C#
#region "JQGrid Actions"
        public JsonResult GetGridData(string sidx, string sord, int rows, int page)
        {
            int pageIndex = page;
            int totalRecords = Convert.ToInt32(_tabmasterService.Count());
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
            IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(sidx, sord, rows, page);
            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (from tm in tabmasters
                        select new
                        {
                            id = tm.colID,
                            cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName }
                        }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection updateExisting)
        {
            int _id = Convert.ToInt32(updateExisting["colID"]);
            TabMasterViewModel editExisting = new TabMasterViewModel();
            editExisting = _tabmasterService.GetSingle(x => x.colID == id);
            try
            {
                UpdateModel(editExisting);
                _tabmasterService.Update(editExisting);
                return Content("Success");
            }
            catch (Exception e)
            {
                return Content(e.Message);
            }
        }
        #endregion 

Note:- Get and Update is successfully working but i have problem in ADD and DELETE. Please anybody help me to write the function for ADD and DELETE?
Posted
Comments
[no name] 15-Jun-11 1:52am    
What is the problem that you are getting? Any error..?

1 solution

Here is complete solution for ADD, EDIT AND DELETE file: index.cshtml

C#
jQuery(document).ready(function () {
        jQuery("#list").jqGrid({
            url: '/TabMaster/GetGridData',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['col ID', 'First Name', 'Last Name'],
            colModel: [
                  { name: 'colID', index: 'colID', width: 100, align: 'left', searchoptions: { sopt: ['eq', 'ne', 'cn']} },
                  { name: 'FirstName', index: 'FirstName', width: 150, align: 'left', editable: true },
                  { name: 'LastName', index: 'LastName', width: 300, align: 'left', editable: true },
                ],
            pager: jQuery('#pager'),
            rowNum: 4,
            rowList: [1, 2, 4, 5, 10],
            sortname: 'colID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: true,
            imgpath: '/scripts/themes/steel/images',
            caption: 'Tab Master Information'
        }).navGrid('#pager', { edit: true, add: true, del: true },
        // Edit options
            {
            savekey: [true, 13],
            reloadAfterSubmit: true,
            jqModal: false,
            closeOnEscape: true,
            closeAfterEdit: true,
            url: "/TabMaster/Edit/",
            afterSubmit: function (response, postdata) {
                if (response.responseText == "Success") {
                    jQuery("#success").show();
                    jQuery("#success").html("Company successfully updated");
                    jQuery("#success").fadeOut(6000);
                    return [true, response.responseText]
                }
                else {
                    return [false, response.responseText]
                }
            }
        },
        // Add options
             {url: '/TabMaster/Create', closeAfterAdd: true },
        // Delete options
               {url: '/TabMaster/Remove' },
               { closeOnEscape: true, multipleSearch: true,
                   closeAfterSearch: true
               }
               );
    });



file: controller.cs

C#
#region "JQGrid Actions"
        public JsonResult GetGridData(string sidx, string sord, int rows, int page)
        {
            int pageIndex = page;
            int totalRecords = Convert.ToInt32(_tabmasterService.Count());
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
            IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(sidx, sord, rows, page);
            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (from tm in tabmasters
                        select new
                        {
                            id = tm.colID,
                            cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName }
                        }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection updateExisting)
        {
            int _id = Convert.ToInt32(updateExisting["colID"]);
            TabMasterViewModel editExisting = new TabMasterViewModel();
            editExisting = _tabmasterService.GetSingle(x => x.colID == id);
            try
            {
                UpdateModel(editExisting);
                _tabmasterService.Update(editExisting);
                return Content("Success");
            }
            catch (Exception e)
            {
                return Content(e.Message);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Remove(string id)
        {
            List<String> ids = new List<String>(id.Split(','));
            for (int i = 0; i < ids.Count; i++)
            {
                int deleteid = Convert.ToInt32(ids[i]);
                TabMasterViewModel deleteExisting = new TabMasterViewModel();
                deleteExisting = _tabmasterService.GetSingle(x => x.colID == deleteid);
                _tabmasterService.Delete(deleteExisting);
            }
            return RedirectToAction("Index");
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection FormValue)
        {
            if (ModelState.IsValid)
            {
                TabMasterViewModel addNew = new TabMasterViewModel();
                addNew.FirstName = FormValue["FirstName"];
                addNew.LastName = FormValue["LastName"];
                _tabmasterService.Add(addNew);
            }
            return RedirectToAction("Index");
        }
        #endregion



HTH
 
Share this answer
 
Comments
Sunasara Imdadhusen 20-Jun-11 7:55am    
100% correct answer! I was solved this before 2 day's. but i appreciate your efforts, and i voted 5!.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900