AngularJS服务对Web api控制器的POST导致Object属性为null

时间:2015-02-08 01:15:01

标签: angularjs asp.net-web-api

我正在尝试形成一个对象并将其发布到web api控制器。行动如下:

        public HttpResponseMessage Post(Contacts contact)
        {
            db.Add(contact);
            db.SaveChanges();

            var response = new HttpResponseMessage(HttpStatusCode.OK);
            return response;
        }

AngularJS控制器帖子如下:

        $scope.submitData = function submitData() {
            console.log($scope.contact);
            $http.post('api/Contacts', $scope.contact);
        }

问题是需要发布的数据具有在控制台中显示为Object的属性(本例中为Addresses)。

enter image description here

在帖子发布后,web api控制器端的所有地址计数为空,但其他普通属性没有相应的值。

enter image description here

如何将对象传递给Web api端可读?

联系人型号:

public partial class Contacts
    {
        public Contacts()
        {
            this.Addresses = new HashSet<Addresses>();
            this.Emails = new HashSet<Emails>();
            this.PhoneNumbers = new HashSet<PhoneNumbers>();
            this.Tags = new HashSet<Tags>();
        }

        public int ContactID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }

        public virtual ICollection<Addresses> Addresses { get; set; }
        public virtual ICollection<Emails> Emails { get; set; }
        public virtual ICollection<PhoneNumbers> PhoneNumbers { get; set; }
        public virtual ICollection<Tags> Tags { get; set; }
    }

我如何填写地址对象:

            <fieldset class="container" ng-repeat="i in getAddress() track by $index">
                <legend>Address {{$index + 1}}</legend>
                <label>Street:</label>
                <input ng-model="contact.Addresses[$index].Street" class=" form-control" type="text" /><br />
                <label>Number:</label>
                <input ng-model="contact.Addresses[$index].Number" class="form-control" type="text" /><br />
                <label>City:</label>
                <input ng-model="contact.Addresses[$index].City" class="form-control" type="text" /><br />
                <label>Country:</label>
                <input ng-model="contact.Addresses[$index].Country" class="form-control" type="text" /><br />
            </fieldset>

0 个答案:

没有答案