在 mvc 中使用 angularjs 和 restful web api 获取数据

Get data using angularjs with restful web api in mvc

本文关键字:web restful api 获取 数据 angularjs mvc      更新时间:2023-09-26

我需要创建一个应用程序,该应用程序从存储过程中获取数据并将其显示在视图中。

当我将 id 从视图传递到 angularjs 控制器时,具有该字段的值,但无法在 web API 中获得相同的值,它显示 null(调用时)。

更具体地说,尝试了各种选项,其中一些如下:

  • 二手 [来自正文]
  • 将 json 中的字符串值从 angularjs 控制器传递到 webapi

因此,当它为 null 时,它总是抛出失败的结果,而不是成功。

请帮忙。

法典:

  • Angularjs (controller)

app.controller('matchDetailController', function($scope, $http) {
  $scope.getAllMatchDetail = function(matchId) {
    var MatchId = matchId;
    $http.get('/api/[Apicontrollername]/GetAllMatchDetail', MatchId).success(function(data) {
      if (data.status == "success") {
      } else {
      }
    }).error(function(error) {
      debugger;
    })
  }
});
<div class="view_container" id="matchDetail" data-ng-controller="matchDetailController" data-ng-init="getAllMatchDetail(@ViewBag.MatchId)">

  • 网络接口

    public 对象 GetAllMatchDetail([FromBody]string MatchId) { SoccerWebviewMatchDetailRepository _SoccerWebviewMatchDetailRepository = new SoccerWebviewMatchDetailRepository();

        List<clsSoccerWebviewMatchDetail> objlstclsSoccerWebviewMatchDetail = _SoccerWebviewMatchDetailRepository.getAllSoccerWebviewMatchDetail(MatchId);
        if (objlstclsSoccerWebviewMatchDetail != null && objlstclsSoccerWebviewMatchDetail.Count() != 0)
        {
            var jsonObject = new
            {
                status = "success",
                objlstclsSoccerWebviewMatchDetail = _SoccerWebviewMatchDetailRepository.getAllSoccerWebviewMatchDetail(MatchId)
            };
            return jsonObject;
        }
        else
        {
            var jsonObject = new
            {
                status = "fail",
                message = "No match details found"
            };
            return jsonObject;
        }
    }
    

检查代码。

首先这是 HttpGet 方法,你需要在 URL 中传递参数。

棱角分明.js

$http.get('/api/[Apicontrollername]/GetAllMatchDetail/'+ MatchId ).success(function(data) {
  if (data.status == "success") {
  } else {
  }
}).error(function(error) {
  debugger;
})

Web API : 删除 [从正文]

public object GetAllMatchDetail(string MatchId) 
{ 
    SoccerWebviewMatchDetailRepository _SoccerWebviewMatchDetailRepository = new SoccerWebviewMatchDetailRepository();
}