开发者

Pass ColID instead of jqGrid Row Index when submit save changes

I have jqGrid with enabled EDIT, DELETE, ADD and VIEW, Now my problem is when i click on edit button then it will success fully open Dialog with edit mode. now when i am submitting my changes then it will pass jqGrid Row Index instead of ColID (ColId is PK with AutoIdentity True). I would like to pass ColID as parameter.

following is my code snippet:

jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        //url: '/TabMaster/GetGridData',
        url: '/TabMaster/DynamicGridData',
        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,
        gridview: 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 (re开发者_运维技巧sponse, 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]
            }
        }
    });
});

here is i am getting JQRowIndex

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection updateExisting)
{
    TabMasterViewModel editExisting = new TabMasterViewModel();
    editExisting = _tabmasterService.GetSingle(x => x.colID == id);
    try
    {
        UpdateModel(editExisting);
        _tabmasterService.Update(editExisting);
        return Content("Success");
    }
    catch
    {
        return Content("Failure Message");
    }
}

Here is the logic to generate JSON response. Method:1 *This is not display any record, even also dosen't fire any exception*

 public JsonResult DynamicGridData(string OrderByColumn, string OrderType, int page, int pageSize)
    {
        int pageIndex = Convert.ToInt32(page) - 1;
        int totalRecords = GetTotalCount();
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
        IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(OrderByColumn, OrderType, page, pageSize);
        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);
    }

The following is working perfectly. (this is working fine but i want to use Method:1 instead of Method:2) Method:2

public ActionResult GetGridData(string sidx, string sord, int page, int rows)
        {
            return Content(JsonForJqgrid(GetDataTable(sidx, sord, page, rows), rows, GetTotalCount(), page), "application/json");
        }
        public int GetTotalCount()
        {
            return Convert.ToInt32(_tabmasterService.Count());
        }
        public DataTable GetDataTable(string OrderByColumn, string OrderType, int page, int pageSize)
        {
            TabMasterListViewModel models = _tabmasterService.GetTabMasterList(OrderByColumn, OrderType, pageSize, page);
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("colID", Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("FirstName", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("LastName", Type.GetType("System.String")));
            foreach (TabMasterViewModel model in models.TabMasterList)
            {
                DataRow dr = dt.NewRow();
                dr[0] = model.colID;
                dr[1] = model.FirstName;
                dr[2] = model.LastName;
                dt.Rows.Add(dr);
            }
            var rows = dt.Rows.Count;
            return dt;
        }
        public static string JsonForJqgrid(DataTable dt, int pageSize, int totalRecords, int page)
        {
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{");
            jsonBuilder.Append("\"total\":" + totalPages + ",\"page\":" + page + ",\"records\":" + (totalRecords) + ",\"rows\"");
            jsonBuilder.Append(":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{\"id\":" + dt.Rows[i][0].ToString() + ",\"cell\":[");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }


I suppose that the origin of your problem is wrong filling of JSON data in the GetGridData. Because your code contain imgpath I could suppose that you use some very old example of jqGrid. In some old examples it was a bug that one used i instead of id in the JSON data. If jqGrid don't find id property in the input data it uses integers 1, 2 and so on as ids. I suppose it is your case. If you includes at least the last lines of your GetGridData we would see that.

I recommend you to look at the UPDATED part of the answer and download the corresponding demo project. It shows not only how to implement data paging, sorting and filtering in ASP.NET MVC 2.0 but shows additionally how to use exceptions to return informations about an error from the ASP.NET MVC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜