【问题标题】:How to get data from db using angularjs and entity framework in mvc如何在 mvc 中使用 angularjs 和实体框架从 db 获取数据
【发布时间】: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


【解决方案1】:

这可能不起作用的原因是因为在您的get 中,您在 Angular 之后添加了 Controller 这个词。

那是不必要的。

这应该可行:

var myApp = angular.module('MyModule', []);
    myApp.controller('DBcontroller', function ($scope, $http) {
    $http.get('/Angular/AllStudents') // added an '/' along with deleting Controller portion
        .then(function (response) {
            $scope.students = response.data
        })
    })

【讨论】:

  • 现在一切都很好,现在原因是数据是通过post-request而不是get-request显示在页面上的。可能是什么原因?
  • @WASIF 抱歉,你能澄清一下你想问什么吗?
  • 在 Angular 中,$http.get() 不会给我数据,但是当我将其更改为 $http.post() 时,它会给我显示数据。
  • @WASIF 也许this 会有所帮助
  • 这很好,它说明了 $http.get() 和 $http.post() 的区别,但我想知道如何通过 $http.get() 获取数据,而不是通过$http.post()
【解决方案2】:

如果你在 web.config 文件中添加这个 Http Get 也可以工作

<configuration>
  <system.web>
    <webServices>
      <protocols>
       <add name="HttpGet"/>
      </protocols>
    </webServices>
  </system.web>
</configuration> 

【讨论】:

    【解决方案3】:

    尝试从$http.get更改为$http.post

    var myApp = angular.module('MyModule', []);
    myApp.controller('DBcontroller', function ($scope, $http) {
        $http.post('/Angular/AllStudents') // added an '/' along with deleting Controller portion
        .then(function (response) {
            $scope.students = response.data
        });
    

    });

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2014-08-31
      相关资源
      最近更新 更多