最近模仿了刘冬大哥的<开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo>之后发现一个问题就是前台datagrid如果删除多行之后,然后再编辑就不能进入对应的行信息.如图:
前台选择多行->删除
删除成功
这里变成了删除的编码,
前台代码
<script type="text/javascript"> $(function () { $('#test').datagrid({ title: '风格信息', iconCls: 'icon-save', fit: true, nowrap: false, striped: true, //collapsible: true, url: '/GenerManage/LoadAllByPage/', sortName: 'GenreId', remoteSort: false, idField: 'GenreId', frozenColumns: [[ { field: 'ck', checkbox: true } ]], columns: [[ { field: 'GenreId', title: '编码', width: '80', align: 'center' }, { field: 'Name', title: '名称', width: '200', align: 'left' }, { field: 'Desctiption', title: '描述', width: '350', align: 'left' } ]], pagination: true, rownumbers: true, toolbar: [{ id: 'btnadd', text: '添加', iconCls: 'icon-add', handler: function () { this.href = '/GenerManage/View/'; } }, { id: 'btnupdate', text: '修改', iconCls: 'icon-save', handler: function () { var rows = $('#test').datagrid('getSelected'); if (rows) { this.href = "/GenerManage/View/" + rows.GenreId; } else { $.messager.alert('提示', '请选择要修改的数据'); return; } } }, '-', { id: 'btncut', text: '删除', iconCls: 'icon-cut', handler: function () { //获取表格选择行 var rows = $('#test').datagrid('getSelections'); //判断是否选择行 if (!rows || rows.length == 0) { $.messager.alert('提示', '请选择要删除的数据!', 'info'); return; }
var parm; //循环给提交删除参数赋值(音乐风格编码) $.each(rows, function (i, n) { if (i == 0) { parm = "idList=" + n.GenreId; } else { parm += "&idList=" + n.GenreId; } }); $.messager.confirm('提示', '是否删除选中数据?', function (r) { if (!r) { return; } //提交 $.post('/GenerManage/Delete/', parm, function (msg) { if (msg.IsSuccess) { $.messager.alert('提示', msg.Message, 'info', function () { //重新加载当前页 $('#test').datagrid('reload'); }); } else { $.messager.alert('提示', msg.Message, 'info') } }); }); } }] }); var p = $('#test').datagrid('getPager'); if (p) { $(p).pagination({ onBeforeRefresh: function () { alert('before refresh'); } }); } }); </script> |
后台代码
//显示创建或者修改信息 public ActionResult View(int? id) { Genre g = (from gener in msdb.Genre where gener.GenreId == id select gener).FirstOrDefault(); if (g == null) g = new Genre(); return View(g); } [HttpPost] public ActionResult Delete(IList<int> idList) { //判断判断是否删除多行数据 if (idList.Count > 1) { foreach (int i in idList) { var gener = from g in msdb.Genre where g.GenreId == i select g; msdb.Genre.DeleteAllOnSubmit(gener); } } else { var gener = from g in msdb.Genre where g.GenreId == idList.First() select g; msdb.Genre.DeleteAllOnSubmit(gener); } //这里删除多条记录的时候提示varchar-int失败所以用上面的办法 // msdb.Genre.DeleteAllOnSubmit(idList.Cast<Genre>().ToList()); msdb.SubmitChanges(); return Json(new { IsSuccess = true, Message = "删除成功" }); } |