使用ASP.Net MVC中的JSON从AngularJS调用MVC控制器

问题描述 投票:1回答:2

JsonResult方法未通过$http调用进行调用,

我正在使用ASP.NET MVC,AngularJS的项目中工作,我正在从AngularJS调用mvc控制器。我从AngularJS调用MVC控制器时得到了一个jsonresult。

这是结果

[
  {
    "Branch_ID": 1,
    "Branch_Name": "sdsds",
    "Branch_Address": "sfsdfsdf",
    "Branch_email": "sdfsdfsdf",
    "Branch_Notes": "sfsffsfd",
    "Branch_Manager": null,
    "Branch_Phone": null,
    "Branch_TimeFrom": "/Date(-2208996000000)/",
    "Branch_TimeTo": "/Date(-2208996000000)/",
    "saturday": false,
    "sunday": false,
    "monday": false,
    "tuesday": false,
    "wednesday": false,
    "thursday": false,
    "friday": false,
    "Departments": null
  }
]

分支机构控制器

public class BranchesController : Controller
   {

    private IRepositoryBase<Branches> BrancheRepository;

    public BranchesController(IRepositoryBase<Branches> brancheRepository)
    {
        this.BrancheRepository = brancheRepository;
    }
    // GET: Branches
    public JsonResult Index()
    {

        var branches =   BrancheRepository.GetAll();

        //if (!String.IsNullOrEmpty(searchString))
        //{
        //    branches = branches.Where(s => s.Branch_Name.ToUpper().Contains(searchString.ToUpper()));
        //}

        return Json(branches, JsonRequestBehavior.AllowGet);
    } 
}

Index.cshtml

<div class="container" ng-controller="branch-controller">
<div class="panel panel-info">
    <div class="panel-heading">
        Branch Details - Grid CRUD operations
    </div>
    <table class="table table-bordered">
        <thead style="background-color:lightblue;">
            <tr>
                <th> Branch Address</th>
                <th> Branch Email</th>
                <th>Branch Name</th>
                <th>Branch Notes</th>
                <th> Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="branche in Branches">
                  <td>{{branche.Branch_ID}}</td>
                <td>{{branche.Branch_Address}}</td>
                <td>{{branche.Branch_email}}</td>
                <td>{{branche.Branch_Name}}</td>
                <td>{{branche.Branch_Notes}}</td>

                <td style="width:200px;">
                    <a href="#" class="btn btn-info">Update</a>
                    <a href="#" class="btn btn-danger">Delete</a>
                </td>
            </tr>
        </tbody>
    </table>
</div>

Module.js

var myapp;
(function () {
    alert("getAllBranches");
    myapp = angular.module('my-branches', []);

Controller.js

myapp.controller('branch-controller', function ($scope, branchService) {

//Loads all branch records when page loads
loadBranches();

function loadBranches() {
    var BrancheRecords = branchService.getAllBranches();

    BrancheRecords.then(function (data) {
        //success

        $scope.Branches = data;
    },
    function (error) {
        console.log(error);
        alert("Error occured while fetching branche list...");
    });
    }
  });

Service.js

myapp.service('branchService',函数($ http){

this.getAllBranches = function () {

    return $http.get("/Branches/Index").then(function (response) {

        return response.data;
    });
};

});

})()
angularjs asp.net-mvc angularjs-http jsonresult
2个回答
0
投票

将服务电话更改为:

var BrancheRecords = branchService.getAllBranches();

BrancheRecords.then(function (data) {
    //success
    $scope.Branches = data;
},
  function (error) {
    console.log(error);
    alert("Error occured while fetching branche list...");
});

将服务更改为:

myapp.service('branchService', function ($http) {

    this.getAllBranches = function () {

        return $http.get("Branches/Index").then(function(response) {
            return response.data;
        });
    };
});

将数据分配给响应对象的data属性。

有关更多信息,请参阅


0
投票

首先,您需要更改代码,如in @georgeawg's answer所示,然后剩下的问题是您使用的属性名称无效。结果应如下所示

<td>{{branche.Branch_Address}}</td>
<td>{{branche.Branch_email}}</td>
<td>{{branche.Branch_Name}}</td>
<td>{{branche.Branch_Notes}}</td>
© www.soinside.com 2019 - 2024. All rights reserved.