Angular Web API PUT模型无法识别

时间:2016-05-19 12:42:10

标签: angularjs json asp.net-web-api2

我在使用Angular将相当复杂的数据模型输入Web API控制器时遇到了麻烦。

这是我们到目前为止所拥有的:

角度控制器:

$scope.saveDelivery = function () {
    var val = $('#run-value', $element)[0].value;
    console.info("Batsman: " + $scope.striker.Name + ", Shot: " + $scope.shot + " for " + val + " runs. ");

    if ($scope.game.Overs[$scope.over - 1].Deliveries == null) {
        $scope.game.Overs[$scope.over - 1].Deliveries = [];
    }

    console.info("Over: " + $scope.over + ", Delivery: " + $scope.delivery);
    if ($scope.delivery > 6) {
        $scope.over++;
        $scope.delivery = 1;
    }
    else {
        $scope.delivery++;
    }

    if ($scope.striker == undefined) {
        alert("No batsman selected");
    }
    else {

        var shot = {};

        shot.Id = 0;
        shot.Runs = $scope.runs;
        shot.Stroke = $scope.shot;
        if ($scope.runs == -5) {
            shot.Dismissal = $scope.shot;
        }
        else {
            shot.Dismissal = null;
        }

        var delivery = {};
        delivery.Id = 0;
        delivery.Number = $scope.delivery;
        delivery.Batter = $scope.striker;
        delivery.Bowler = $scope.nonstriker;
        delivery.Shot = shot;


        $scope.game.Overs[$scope.over - 1].Deliveries.push(delivery);
        console.info($scope.game);

        $http.put('/api/games/:id', { id: $scope.game.Id, game: $scope.game }).success(function (data, status, headers, config) {
            console.info(data);
        });
        //$http.post('/score/testing', { Test: "This is a test" }).success(function (data, status, headers, config) {
        //    console.info(data);
        //});
    }
}

API控制器:

// PUT: api/Games/5
[HttpPut("{id}")]
public IActionResult PutGame(int id, [FromBody] Game game)
{
    if (!ModelState.IsValid)
    {
        return HttpBadRequest(ModelState);
    }
    ... etc...

我在Fiddler中抓取的原始JSON数据看起来像这样(抱歉格式化):

{"id":1,"game":{"Id":1,"Date":"2016-04-20T20:30:00","Team":{"Id":"a5abca2a-0e05-11e6-8596-4f0c9eb370be","Name":"Yellow Duckies","Players":null},"Opposition":"Miller Studios","Overs":[{"Id":1,"Number":1,"Innings":0,"Deliveries":[{"Id":0,"Number":2,"Batter":{"Id":"1a903e4a-0f8a-11e6-99ad-b3c19db370be","Email":"phil@email.address.here","Name":"Phil"},"Bowler":{},"Shot":{"Id":0,"Runs":2,"Stroke":2,"Dismissal":null}}]},{"Id":2,"Number":2,"Innings":0,"Deliveries":null},{"Id":3,"Number":3,"Innings":0,"Deliveries":null},{"Id":4,"Number":4,"Innings":0,"Deliveries":null},...

如果我将Web API PUT参数更改为普通object,它会很好地拾取正文,因此我猜测对象的解析方式存在问题,但是如果我能看出问题出在哪里,我该死的。这是C#模型:

namespace IndoorCricket.Models
{
    public enum Innings { Batting, Bowling }

    public class Game
    {
        public int Id { get; set; }
        public DateTime Date { get; set; }
        public virtual Team Team { get; set; }
        public string Opposition { get; set; }

        public virtual ICollection<Over> Overs { get; set; }
    }


    public class Over
    {
        public int Id { get; set; }
        public int Number { get; set; }
        public Innings Innings { get; set; }

        //public virtual Game Game { get; set; }
        public virtual ICollection<Delivery> Deliveries { get; set; }
    }

    public class Delivery
    {
        public int Id { get; set; }
        public int Number { get; set; }
        public virtual Shot Shot { get; set; }

        public virtual Player Bowler { get; set; }
        public virtual Player Batter { get; set; }
    }

    public class Shot
    {
        public int Id { get; set; }
        public int Runs { get; set; }
        public Stroke Stroke { get; set; }
        public Dismissal Dismissal { get; set; }
    }

    public enum Stroke
    {
        Out     = -5,
        Dotball = 0,
        Single  = 1,
        Two     = 2,
        Three   = 3,
        Four    = 4,
        Five    = 5,
        Seven   = 7
    }

    [Flags]
    public enum Dismissal
    {
        Caught  = 1,
        Bowled  = 2,
        Runout  = 4,
        Stumped = 8,
        Mankad  = 16,
        LBW     = 32
    }
}

任何人都有更好的眼睛可以发现问题吗?

2 个答案:

答案 0 :(得分:0)

它试图从正文构建的typings类不是根对象。 .NET将检测到game列,并且无法映射到类id。您是否能够在JSON中创建Game根?因为看起来你正在传递&#34; id&#34;无论如何进入API的控制器终点?

此外,您指定的gameTeam类在哪里?

答案 1 :(得分:0)

最终通过从强类型&#34;游戏&#34;更改API参数类型来解决该问题。反对香草object。从那里,我使用JSON.Net对对象进行反序列化并检查发送的内容。

因为我在嵌套的JSON对象中有一定程度的深度,所以我可以看到期望值的属性传递给它的是null。

我暂时离开了它(虽然感觉很蠢,我想通过进一步的工作):

    public IActionResult PutGame(int id, [FromBody] object game)
    {
        JObject obj = JsonConvert.DeserializeObject<JObject>(game.ToString());
        Game g = obj.Root.First.Value<JToken>().First.ToObject<Game>();
    ...