I am newbie in learning AngularJS. Can anyone help me to understand how to get data from database using angularjs and entity-framework in mvc5.
Let's say we have data table named:
tbl_student {rollno, name}
and we want all the students data from the database. So what should be the most understandable way for new learners.
Angular-Code
var myApp = angular.module("MyModule", []);
myApp.controller("DBcontroller", function ($scope, $http) {
$http.get("AngularController/AllStudents")
.then(function (response) {
$scope.students = response.data
})
})
Html-code in file named "Test.cshtml"
<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>
In MVC Controller
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);
}
}
}
AngularController
.. so it should just be$http.get("/Angular/AllStudents")
– BviLLe_Kid Jun 22 '16 at 14:56