您好我在MVC中开始使用JQgrid并且我在JQgrid中执行CRUD操作,当我加载页面时,网格将填充所需数据,但是当我删除或编辑时,我收到错误,当我检查它时,错误是编辑操作所期望的用户ID尚未从视图初始化。 我的观点看起来像这样
<table id="jQGridDemo">
</table>
<div id="jQGridDemoPager">
</div>
<script type="text/javascript">
jQuery("#jQGridDemo").jqGrid({
url: 'RTGUser/GetUserDetails',
datatype: "json",
colNames: ['Id', 'Name', 'Designation', 'City'],
colModel: [
{ name: 'Id', index: 'Id', width: 40, align: 'center', sortable: true, editable: false, key: true, editrules: { required: true} },
{ name: 'Name', index: 'Name', width: 40, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
{ name: 'Designation', index: 'Designation', width: 400, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
{ name: 'City', index: 'City', width: 150, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} }
],
mtype: 'GET',
loadonce: true,
rowList: [5, 10, 20, 30],
pager: '#jQGridDemoPager',
sortname: 'Id',
viewrecords: true,
sortorder: 'desc',
caption: "List Of Users",
editurl: "RTGUser/EditUser",
onSelectRow: function(Id){
if(id && id!==lastsel){
jQuery('#rowed3').jqGrid('restoreRow',lastsel);
jQuery('#rowed3').jqGrid('editRow',Id,true);
lastsel=Id;
}
},
});
jQuery("#jQGridDemo").jqGrid('navGrid', '#jQGridDemoPager',
{ edit: true, add: true, del: true, search: true },
{ url: "/RTGUser/EditUser/", closeAfterEdit: true, beforeShowForm: function (formid) { $("#Id",formid).hide(); } },
{ url: "/RTGUser/AddNewUser/", closeAfterAdd: true, beforeShowForm: function (formid) { $("#Id", formid).hide(); } },
{ url: "/RTGUser/DeleteUser/" }, {});
$("#search").filterGrid("#grid", {
gridModel: false,
filterModel: [{
label: 'Search',
name: 'search',
stype: 'text'
}]
});
</script>
我的控制器
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditUser(string userId, string name, string desig, string city)
{
int uId = Convert.ToInt32(userId);
try
{
var query = from u in db.Users
where u.Id.Equals(userId)
select u;
User i = db.Users.SingleOrDefault(u => u.Id == uId);
if (!(i == null))
{
var user = query.First();
user.Name = name;
user.Designation = desig;
user.City = city;
db.SaveChanges();
}
return Json(true);
}
catch (Exception)
{
// Do some error logging stuff, handle exception, etc.
return Json(false);
}
}
如何从我的View中获取mt id。 在此先感谢
答案 0 :(得分:0)
将您的操作签名更改为:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditUser(string Id, string Name, string Designation, string City)
{
}
没有神奇的方法将值附加到您的参数,绑定器将只是按名称匹配它们。