AngularJS の $resource を使用して Web Api バックエンドを呼び出す方法を学習しています。オブジェクト階層を基準として渡し、IEnumerable<Program>
. 基準の例を次に示します。
$scope.criteria = {
Categories:[
{
Name: "Cat1",
Options: [
{Text: "Opt1", Value: true},
{Text: "Opt2", Value: false}
]
},
{
Name: "Cat2",
Options: [
{Text: "Opt3", Value: true},
{Text: "Opt4", Value: false}
]
}
]
}
C# のサーバーで同じオブジェクトを定義しています。
public class CriteriaModel
{
public IEnumerable<CriteriaCategory> Categories { get; set; }
}
public class CriteriaCategory
{
public string Name { get; set; }
public IEnumerable<CriteriaOption> Options { get; set; }
}
public class CriteriaOption
{
public string Text { get; set; }
public bool Value { get; set; }
}
$resource の設定方法は次のとおりです。
angular.module('my.services')
.factory('api', [
'$resource',
function ($resource) {
return {
Profile: $resource('/api/profile/:id', { id: '@id' }),
Settings: $resource('/api/settings/:id', { id: '@id' }),
Program: $resource('/api/program/:id', { id: '@id' })
};
}
]);
そして、私はそれを次のように呼びます:
api.Program.query({ criteria: $scope.criteria }, function (response) {
$scope.programs = response;
});
何を試してもnull
、条件パラメーターとして取得するか、アクションがまったく実行されません。問題が角度、Web API、またはその両方にあるのかどうかはわかりません。アクションは次のとおりです。
public class ProgramController : ApiController
{
public IEnumerable<Program> GetByCriteria([FromUri]CriteriaModel criteria)
{
// Either criteria is null or this action doesn't even get
// executed depending on what I try.
}
}
AngularJS $resource と Web Api を使用してアイテムを検索して返すための実用的な例を得るのを手伝ってくれる人はいますか?