【问题标题】:ASP.NET MVC Display SQL Server table dataASP.NET MVC 显示 SQL Server 表数据
【发布时间】:2019-07-23 17:02:09
【问题描述】:

在我学习 SQL 的第二次旅途中,我一直在学习一个教程,但由于某种原因,我无法让我的表格在我的页面上显示其数据。

在代码的 ready(function) 部分,我尝试使用 3 种不同的方式来调用列名(因为它们中有一个空格,这是我的愚蠢错误!)看看哪一种有效,但我有感觉代码的#CBR 方面没有正确联系数据库? 我错过了什么明显的东西吗?

我已经从下面的视图页面和控制器中粘贴了代码。

欢迎所有帮助!并提前感谢! :)

@{
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>

和控制器摘录:

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);
        }
    }

【问题讨论】:

  • 浏览器控制台有异常吗?运行代码时会发生什么?任何屏幕截图都会有所帮助。调用localhost:blabla/home/GetSafe 的结果是什么?有结果吗?
  • 我可以很好地打开页面,只是没有显示表格中的数据。如果有帮助,我添加了一些屏幕截图以提供更多名称和信息等?后者是我打开页面时的结果:)
  • 可以加个localhost:57444/home/GetSafe的ss吗?
  • 看看你的控制台日志。按 F12 查看页面。更多详情:developers.google.com/web/tools/chrome-devtools/console
  • 找出你的逻辑在哪一步失败了。通过设置断点来测试您的服务器代码是否真的在提取数据。如果可行,请测试您的 JavaScript 代码是否正确捕获数据。等等等等。

标签: c# jquery sql-server asp.net-mvc


【解决方案1】:

首先, 用这个改变你的 jquery 脚本:

<script src="code.jquery.com/jquery-3.1.1.min.js"></script> 

看来,您的服务器上没有该文件。

第二, c# 异常说,您关闭了连接,并且无法从 db 中选择新项目。试试这个连接。有时当你有虚拟属性(延迟加载)时会发生这种情况

    var safe = dc.Safes.OrderBy(a => a.User_ID).Select(s=>
new{SafeID=s.SafeID,
DepartmentID=s.DepartmentID, 
User_ID=s.User_ID}
).ToList();

【讨论】:

  • 我用那个替换了 。并完成了以下操作,但产生相同的错误?事实上,那个 Jquery 脚本在 VS 中标记了一个黄色的“在服务器上找不到”警告?我将添加脚本库的屏幕截图。
  • 值得注意的是...尝试localhost:blah/Home/GetSafe时,它现在确实产生了数据库数据! ?
  • 但是普通页面还是不行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多