SCRIPTS:
<script type="text/javascript">
$(document).ready(function () {
$("#grid").jqGrid({
url: '@Url.Action("GetAllCategories", "Admin")',
datatype: "json",
mtype: 'GET',
colNames: ['sno','Kategori Adı', 'Sıra No', 'Vitrin'],
colModel: [
{ name: 'sno', index: 'sno', editable: false, hidden: true },
{ name: 'Name', index: 'Name', editable: true },
{ name: 'OrderNo', index: 'OrderNo', editable: true },
{ name: 'IsShowcase', index: 'IsShowcase', width: 100, editable: true, sortable: false }
],
jsonReader: {
repeatitems: false
},
rowNum: 10,
rowList: [10, 20, 30, 40, 50],
pager: jQuery('#gridpager'),
sortname: 'CategoryName',
viewrecords: true,
sortorder: "asc",
id: "sno",
width: 710,
height: 300,
editurl: '@Url.Action("_EditCategory", "Admin")'
}).navGrid('#gridpager');
});
</script>
CONTROLLER
[HttpPost]
public ActionResult _EditCategory(CategoriesViewModel categoriesViewModel)
{
Categories category = entity.Categories.SingleOrDefault(x => x.sno == categoriesViewModel.sno);
category.IsShowcase = categoriesViewModel.IsShowcase;
category.Name = categoriesViewModel.Name;
category.OrderNo = categoriesViewModel.OrderNo;
try
{
entity.Categories.Add(category);
entity.SaveChanges();
}
catch (Exception ex) { }
return PartialView(categoriesViewModel);
}
I debugged it. I cant post only sno of model(sno is the primary key of Model and uniq). Model is posted with parameters, but only sno is not posted.
How can I achieve this? THANKS.
First thing that concerns me in your code is the
idoption which you use – jqGrid doesn’t have such option. If that option was suppose to make jqGrid resolve the row id out of your model, than you should have usejsonReaderfor this purpose:Now assuming that your rows ids are bound properly, you can use
prmNamesoption to tell jqGrid that it should POST row id under sno name:This should resolve your issue.
P.S. Most probably you don’t need sno column in your columns model (and columns names) definition as you use it only for row id.