将http.get与参数

时间:2015-10-02 08:29:32

标签: angularjs asp.net-mvc asp.net-web-api

我有一些带有行的ui-grid。 我添加了一个带有链接的列来访问行的详细信息并在新的(angularjs)视图中显示它。 新视图从http.get命令获取其数据。 有没有办法转移"选择的行"参数到新视图,以便http.get获取正确的详细信息? 目前我使用MVC路由,因此在两个角度部分之间有一个控制器。

UI网:

 $scope.gridOptions2 = {

        enableFiltering: true,
        treeRowHeaderAlwaysVisible: false,
        rowHeight: 100,
        columnDefs: [
            { name: 'Trigraph', field: 'ZeigeTrigraphen', width: '10%' },
            { name: 'Titel', field: 'Titel', cellTemplate: '<td style="word-wrap:break-word;padding:5px;">{{ COL_FIELD }}</td>' },
            {name: 'Aktionen',field:'AlarmkalenderId',cellTemplate:'<a href="/Details/{{COL_FIELD}}" id="Details" class="btn btn-success" )"><i class="glyphicon glyphicon-edit"</a>'}
        ],
        onRegisterApi: function (gridApi2) {
            $scope.gridApi2 = gridApi2;
        }
    };

MVC控制器:

 public ActionResult Details(int id)
        {Viewmodel = ViewModelService.getViewmodel(id);
            return View(Viewmodel)
        }

新视图中的http.get(上面返回的一个):

 $http.get('/api/Alarmkalender/HoleAlarmmassnahme').then(function (resp) {

            $scope.gridOptionsEinzelmassnahmen.data = resp.data.IndexEinzelmassnahmen;
            $scope.data=resp.data;
            $log.info(resp);
        });

WebApiController方法:

public async Task<IHttpActionResult> HoleAlarmmassnahme(int alarmmassnahmeId=1)
        {
detailsAlarmmassnahmeViewModel = //getting Viewmodel from a Controller/Service

            return Ok(detailsAlarmmassnahmeViewModel);
        }

路线:

    public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "404-PageNotFound",
            // This will handle any non-existing urls
            url: "{*url}",
            // "Shared" is the name of your error controller, and "Error" is the action/page
            // that handles all your custom errors
            defaults: new { controller = "Shared", action = "Error" }
        );
    }

1 个答案:

答案 0 :(得分:0)

您无法在GET请求中传递数据,但您可以在请求中添加查询字符串:

$http({
  url: "/api/Alarmkalender/HoleAlarmmassnahme", 
  method: "GET",
  params: {gridRow: YOUR_GRID_ROW_HERE}
});

或直接在网址字符串中添加您的查询

$http.get("/api/Alarmkalender/HoleAlarmmassnahme?gridrow=YOUR_GRID_ROW_HERE")