Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC3
Here is the JQGrid setup information
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
#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 14-Jun-11 0:38am
Comments
Start Shining at 15-Jun-11 1:52am
   
What is the problem that you are getting? Any error..?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is complete solution for ADD, EDIT AND DELETE file: index.cshtml
 
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
 
#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
  Permalink  
Comments
Sunasara Imdadhusen at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks for this solution , I got what i want after a full day of search in google.
My Vote to u is full...
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 24 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100