如何使用asp.net mvc删除jqgrid中的多个寄存器?
Posted
技术标签:
【中文标题】如何使用asp.net mvc删除jqgrid中的多个寄存器?【英文标题】:How to delete multiples registers in jqgrid using asp.net mvc? 【发布时间】:2015-05-15 23:48:02 【问题描述】:您能帮我删除在我的 jqgrid 中选择的多条记录吗?我尝试了很多方法,但到目前为止都没有成功。我会感谢任何可以帮助我的人。
jQuery("#grid-table").jqGrid(
//direction: "rtl",
url: "/Lojas/GetLojas",
datatype: 'json',
mtype: 'Get',
height: '100%',
colNames: [ ' ',
'Name',
'Description'
],
colModel: [
name: 'myac', index: '', width: 65, fixed: true, sortable: false, resize: false,
formatter: 'actions',
formatoptions:
keys: true,
delOptions: recreateForm: true, url: '/Lojas/Delete', beforeShowForm: beforeDeleteCallback ,
editformbutton: true, editOptions: recreateForm: true, url: '/Lojas/Edit', closeAfterEdit: true, beforeShowForm: beforeEditCallback, closeOnEscape: true
,
key: true, hidden: true, name: 'Id', index: 'Id', sorttype: "int", editable: false ,
key: false, name: 'Name', index: 'Name', editable: true,
key: false, name: 'Description', index: 'Description', editable: true
],
viewrecords: true,
loadonce: true,
rowNum: 10,
rowList: [5, 10, 15],
jsonReader:
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
Id: "0"
,
pager: pager_selector,
altRows: true,
autowidth: true,
multiselect: true,
multiboxonly: true,
sortorder: "desc",
multiboxonly: true,
caption: "Lojas Cadastradas"
);
//navButtons
jQuery("#grid-table").jqGrid('navGrid', pager_selector,
edit: true,
add: true,
del: true,
search: true,
refresh: true,
view: true,
,
url: '/Lojas/Edit',
closeOnEscape: true,
closeAfterEdit: true,
recreateForm: true
,
url: '/Lojas/Create',
closeOnEscape: true,
closeAfterAdd: true,
recreateForm: true
,
url: '/Lojas/Delete',
closeOnEscape: true,
closeAfterDelete: true,
recreateForm: true
,
//search form
recreateForm: true,
closeOnEscape: true,
closeAfterSearch: true,
multipleSearch: true
,
//view record form
recreateForm: true
)
我的控制器中的代码:
public ActionResult Delete(Loja loja)
Loja lojaToDelete = db.Lojas.Find(loja.Id);
if (lojaToDelete == null)
return HttpNotFound();
db.Lojas.Remove(lojaToDelete);
db.SaveChanges();
return View(loja);
【问题讨论】:
【参考方案1】:我建议你把Delete函数public ActionResult Delete(Loja loja)
的原型改成
public void Delete(string id)
您的代码中的主要问题如下。对应于the documentation jqGrid post id
参数到url: '/Lojas/Delete'
。您可以使用prmNames
重命名id
参数的名称。在这种情况下,您可以使用prmNames: id: "Id"
,但这并不是必需的。
如果需要删除多行,则id
字符串将以逗号分隔,您可以使用类似
public void Delete(string id)
var ids = id.Split(',');
foreach (lojaId in ids)
Loja lojaToDelete = db.Lojas.Find(lojaId);
if (lojaToDelete == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
db.Lojas.Remove(lojaToDelete);
db.SaveChanges();
【讨论】:
非常感谢您的解决方案,奥列格!我刚刚更改了您的代码以将字符串转换为整数以在数据库中查找记录,并且此代码运行良好!谢谢!!!最终代码:public void Delete(string id) var ids = id.Split(','); foreach (var lojaId in ids) int idLoja = Convert.ToInt32(lojaId); Loja lojaToDelete = db.Lojas.Find(idLoja); if (lojaToDelete == null) throw new HttpResponseException(HttpStatusCode.NotFound); db.Lojas.Remove(lojaToDelete); db.SaveChanges();
@David:不客气!我只是写了代码没有测试它。我建议您验证抛出的异常在您的环境中是否正确,或者您应该使用另一个。您可以使用errorTextFormat
回调自定义删除错误客户端显示的文本。可以在服务器端自定义异常序列化(参见the old answer 中的HandleJsonExceptionAttribute
)。以上是关于如何使用asp.net mvc删除jqgrid中的多个寄存器?的主要内容,如果未能解决你的问题,请参考以下文章
jqGrid postdata 发送空值(mvc3 asp/net)
datetimepicker jqgrid asp.net mvc