JqGrid 編集機能はグリッドで正常に動作していますが、削除機能は常にレコード ID を 0 として渡します。
@model Shop.Models.ShopModels.lstMainCategory
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
<script type="text/javascript">
var myGrid = $('#list');
$(document).ready(function () {
$.jgrid.nav.addtext = "Add";
$.jgrid.nav.edittext = "Edit";
$.jgrid.nav.deltext = "Delete";
$.jgrid.edit.addCaption = "Add Main Category";
$.jgrid.edit.closeAfterAdd = true;
$.jgrid.edit.closeAfterEdit = true;
$.jgrid.edit.editCaption = "Edit Main Category";
$.jgrid.del.caption = "Delete Main Category";
$.jgrid.del.msg = "Delete selected Main Category?";
$.extend($.jgrid.edit, {
beforeSubmit: function () {
$(this).jqGrid("setGridParam", { datatype: "json" });
return [true, "", ""];
}
});
$("#list").jqGrid({
url: '/MainCategory/DynamicGridData/',
datatype: 'json',
mtype: 'GET',
colNames: ['Edit', 'Main Category'],
colModel: [
{ name: 'MaincategoryID', index: 'MaincategoryID', width: 40, align: 'left', /* key: true,*/editable: true, editrules: { edithidden: false }, hidedlg: true, hidden: true },
{ name: 'MainCategoryName', index: 'MainCategoryName', search: true, width: 700, align: 'left', editable: true, edittype: 'text', editrules: { required: true }, formoptions: { elmsuffix: ' *'} }],
pager: jQuery('#pager'),
editurl: '@Url.Action("Update", "MainCategory")',
rowNum: 10,
width: '100%',
height: '100%',
rowList: [5, 10, 20, 50],
viewrecords: true,
loadonce: true,
ignoreCase: true,
caption: 'Main Categories'
}).navGrid('#pager', { edit: true, add: true, del: true, search: true, searchtext: "Search" });
$("#list").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: 'cn' });
$("#list").setGridParam({ data: results.rows, localReader: reader }).trigger('reloadGrid');
</script>
コントローラ:
public ActionResult Update(lstMainCategory viewModel, FormCollection formCollection)
{
var operation = formCollection["oper"];
lstMainCategory mcat = new lstMainCategory();
rep = new MaincategoryRepository();
if (operation.Equals("add"))
{
mcat.MainCategoryName = viewModel.MainCategoryName;
rep.Create(mcat);
rep.Save();
}
else if (operation.Equals("edit"))
{
mcat.MaincategoryID = viewModel.MaincategoryID;
mcat.MainCategoryName = viewModel.MainCategoryName;
rep.Edit(mcat);
rep.Save();
}
else if (operation.Equals("del"))
{
rep.Delete(viewModel.MaincategoryID);//here MaincategoryID is always 0 so it's not working
rep.Save();
}
return Content("true");
}
編集
MainCategoryID MainCategory
16 aaa
17 bbb
リポジトリ:
public object DynamicGridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
{
dc = new ShopDataContext(DBConnection.Connection);
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = dc.tblMainCategories.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
List<MainCategory> lst = (from tbm in dc.tblMainCategories
select new MainCategory
{
MaincategoryID=tbm.MainCategoryID,
MainCategoryName=tbm.MainCategory,
DeleteID=tbm.MainCategoryID
}).ToList<MainCategory>().OrderBy(x=>x.MainCategoryName).ToList();
List<MainCategory> Flst;
if (_search && searchField == "MainCategory")
{
Flst = lst.Where(r => r.MainCategoryName.Contains(searchString)).OrderBy(x => x.MainCategoryName).ToList().ToList();
}
else
Flst = lst;
var GridData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from mcat in Flst
select new
{
i = mcat.MaincategoryID,
cell = new string[] { mcat.MaincategoryID.ToString(), mcat.MainCategoryName.ToString(),mcat.DeleteID.ToString() }
}).ToArray()
};
return GridData;
}