[英]ASP.NET MVC Display SQL Server table data
On my second hitch on my journey to learning SQL, I've been following a tutorial but for some reason I cannot get my table to display its data on my page. 在学习SQL的第二个关键时刻,我一直在遵循教程,但是由于某种原因,我无法使表在页面上显示其数据。
On the ready(function) part of the code, I have tried using 3 different ways of calling the column names (as they have a space in them, foolish error of mine!) to see which one works, but I have a feeling its the #CBR aspect of the code not contacting the database properly? 在代码的ready(function)部分,我尝试使用3种不同的方式来调用列名(因为它们中有空格,这是我的愚蠢错误!),看哪一种有效,但我有一种感觉代码的#CBR方面未正确联系数据库? Have I missed something obvious?
我错过了明显的事情吗?
I have pasted the code from my View page and Controller below. 我从下面的“视图”页面和“控制器”中粘贴了代码。
All help kindly received! 所有的帮助都很好! and Thanks in advance!
并预先感谢! :)
:)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Safes</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
/>
<link rel="stylesheet"
href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
</head>
<body>
<div style="width:90%; margin:0 auto">
<table id="CBR">
<thead>
<tr>
<th>Safe ID</th>
<th>Department ID</th>
<th>Safe Owner ID</th>
</tr>
</thead>
</table>
</div>
<script src="~/Scripts/jquery-3.1.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script
src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js">
</script>
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script>
$(document).ready(function () {
var otable = $('#CBR').DataTable({
"ajax": {
"url": '/home/GetSafe',
"type": "get",
"datatype": "json"
},
"columns": [
{ "data": "Safe ID", "autoWidth": true },
{ "data": "[Department ID]", "autoWidth": true },
{ "data": "User_ID", "autoWidth": true }
]
})
})
</script>
</body>
</html>
And Controller excerpt: 和控制器摘录:
public ActionResult Safes()
{
ViewBag.Message = "Your Safe Page";
return View();
}
public ActionResult GetSafe()
{
using (CBREntities dc = new CBREntities())
{
var safe = dc.Safes.OrderBy(a => a.User_ID).ToList();
return Json(new { data = safe }, JsonRequestBehavior.AllowGet);
}
}
First, Change your jquery script with this one: 首先,使用以下代码更改您的jquery脚本:
<script src="code.jquery.com/jquery-3.1.1.min.js"></script>
Seems, You don't have that file on your server. 似乎,您的服务器上没有该文件。
Second, the c# exception says, you closed your connection and you can't select new items from db. 其次,c#异常表明,您关闭了连接,并且无法从数据库中选择新项目。 Try this with connection.
尝试使用连接。 Sometimes this happen when you have virtual property(lazy loading)
有时,当您具有虚拟属性时会发生这种情况(延迟加载)
var safe = dc.Safes.OrderBy(a => a.User_ID).Select(s=>
new{SafeID=s.SafeID,
DepartmentID=s.DepartmentID,
User_ID=s.User_ID}
).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.