1

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);
        }
    }
}
3
  • are you receiving any error messages? Commented Jun 22, 2016 at 13:09
  • it says "Failed to load resource: the server responded with a status of 404 (Not Found)" and on clicking on error it says "The resource cannot be found." Commented Jun 22, 2016 at 14:54
  • 1
    Take Controller off of AngularController.. so it should just be $http.get("/Angular/AllStudents") Commented Jun 22, 2016 at 14:56

3 Answers 3

2

The reason why this probably isn't working is because in your get you are adding the word Controller after Angular.

That is unnecessary.

This should work:

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
        })
    })
Sign up to request clarification or add additional context in comments.

5 Comments

now everything is fine, now the reason is that data is displayed on page through post-request but not by get-request. what could be the reason?
@WASIF sorry, could you clarify what you are trying to ask?
In angular, $http.get() does not give me data but when I change it to $http.post() it gives me data on display.
@WASIF maybe this will help
this is fine and it is telling about difference about $http.get() and $http.post() but I want to know how to get data through $http.get(), not by $http.post()
0

If you add this in web.config file Http Get will also work

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

Comments

-1

try to change from $http.get to $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
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.