【发布时间】:2016-10-24 09:14:18
【问题描述】:
我是学习 AngularJS 的新手。谁能帮我了解如何在 mvc5 中使用 angularjs 和实体框架从数据库中获取数据。
假设我们有一个名为:tbl_student {rollno, name}
的数据表,我们想要数据库中的所有学生数据。那么对于新学习者来说,最容易理解的方式应该是什么。
角度代码
var myApp = angular.module("MyModule", []);
myApp.controller("DBcontroller", function ($scope, $http) {
$http.get("AngularController/AllStudents")
.then(function (response) {
$scope.students = response.data
})
})
名为“Test.cshtml”的文件中的 HTML 代码
<body ng-app="MyModule">
<table ng-controller="DBcontroller">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Institute</th>
</tr>
</thead>
<tr ng-repeat="student in students">
<td>{{ student.Name }}</td>
<td>{{ student.City }}</td>
<td>{{ student.Institute }}</td>
</tr>
</table>
</body>
在 MVC 控制器中
public class AngularController : Controller
{
// GET: Angular
public ActionResult Test()
{
return View();
}
public JsonResult AllStudents()
{
using (EducationContext context = new EducationContext())
{
var students = context.tbl_Student.ToList();
return Json(students, JsonRequestBehavior.AllowGet);
}
}
}
【问题讨论】:
-
您是否收到任何错误消息?
-
它显示“加载资源失败:服务器响应状态为 404(未找到)”,点击错误时显示“找不到资源”。
-
把控制器从
AngularController.. 所以它应该只是$http.get("/Angular/AllStudents")
标签: angularjs asp.net-mvc entity-framework asp.net-mvc-5