新建一个asp.net的MVC项目,在.cshtml文件中添加datagrid插件,代码片段如下所示:
<table id="dg" class="easyui-datagrid" title="DataGrid Complex Toolbar" style="width: 700px; height: 250px"
data-options="rownumbers:true,singleSelect:true,fitColumns:'true',pagination:'true',url:'/Student/GetStudentInformation',toolbar:'#toolbar'">
<thead>
<tr>
<th data-options="field:'StudentName',width:80">StudentName</th>
<th data-options="field:'StudentNumber',width:100">StudentNumber</th>
<th data-options="field:'Department',width:80,align:'right'">Department</th>
<th data-options="field:'Birthday',width:80,align:'right'">Birthday</th>
</tr>
</thead>
</table>
在.cshtml文件的代码中有一个按钮,点击这个按钮会调用destroyStudent函数,删除选中的一行数据。destroyStudent函数的代码如下所示:
<head>
<script type="text/javascript" src="~/Scripts/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.easyui.min.js"></script>
<script type="text/javascript">
function destroyStudent() {
var row = $('#dg').datagrid('getSelected');
if (row) {
$.messager.confirm('Confirm', 'Are you sure you want to destroy this user?', function (r) {
if (r) {
$.ajax({
dataType: 'json',
type: 'POST',
url: '@Url.Content("~/Student/DeleteStudentInformation")',
data: "StudentId=" + row.StudentId,
success: function (data, status, xhr) {
$('#dg').datagrid('reload');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('删除数据失败');
//commit(false);
}
});
}
});
}
}
</script>
</head>
运行之后能够从数据库中获取数据显示在datagrid表格中,但是当点击“删除”按钮调用“destroyStudent”函数时,第8行一直报错“对象不支持‘datagrid’属性或方法”,这是为什么呢?谢谢。
第一次是基于“基本”模板新建的MVC4应用程序,后来新建了一个“空白”模板的MVC应用程序,再把代码复制进去,就没报错了。
我观察过使用“基本”模板与使用“空白”模板新建的项目中页面之间的差别,发现两者的html代码不同,前者的代码自动添加了外层的html元素,引入了一些js和css文件,而后者没有。
<script type="text/javascript" src="~/Scripts/jquery.datagrid.js"></script> 这个可以去掉 把这条引用注释掉也还是报同样的错误啊,求指教。
你检查下 min.js的路径 还不行 重新下载个easyui压缩包吧
<tr>
<th data-options="field:'StudentName',width:80">StudentName</th>
<th data-options="field:'StudentNumber',width:100">StudentNumber</th>
<th data-options="field:'Department',width:80,align:'right'">Department</th>
<th data-options="field:'Birthday',width:80,align:'right'">Birthday</th>
</tr>
grid里没有这个"StudentId“字段。