locked
Posting object from angularjs to web api. Object is empty RRS feed

  • Question

  • User-341994687 posted

    Hi, 

    I was wondering if anyone could help. I am posting data from angularjs to web api. When I look in fiddler there is data there but when I enter the web api method the object is there but none of the values are there. 

    The web request goes through like this: 

    POST http://localhost:17098/api/ClientDepartments/SaveDepartment HTTP/1.1
    Host: localhost:17098
    User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
    Accept: application/json, text/plain, */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Content-Type: application/json;charset=utf-8
    Authorization: Bearer vadBWcT0lKspvwfZkmCZNR8m3gZqoa8WsSZ--RbyXPtjHPWmOOkgvCzTQMuZY3ZgHUv8mAWTefTceML1lt6JyzvbHiRuwlSVXTmCyFcMBN7XZhvNwO74Pb1BOCK9Govd6EgpgPkqw8Rswd4OUVfyUzQY8nf6IdlthZuT3EMZfXdCqWmoiyCIbF_cV74yaB_VA17Glh5y-CxavZkJadQo-lr-ymnPAnkndGAHUsT8WwSW3Ymjx-6nFHXssXB0c043mhArviJDJGvQCDwu76SGhjeBmKm_8Y9-O4RuofgrrUIuAtZBk8RKiEiXaZSFvJdz0IGO5NgFrR-RNRs6otd5i1CN9LG9xsKuLf2O4dDJPSx9wIHNJH1RRXLIF8V_7DHJQyclpbzxHesIHEt6b-6uGqvwT_pW2ySi_JdVAtIRIJ9r4JiM5b9QwcFBTT5rkYqCCKjEYXIGxOH2TiHaNYXMy3R0RQhtlrcC0aCbbUrv9phW6Rv-
    Referer: http://localhost:34059/Client/Details
    Content-Length: 117
    Origin: http://localhost:34059
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache
    
    {"depart":{"Address":{"Town":"asdf","AddressLine1":"asdjflk","AddressLine2":"jasdflk"},"VatNum":"sdf","Name":"asdf"}}

    But when I enter the method with the following signature the object is empty. Not null. Just empty. 

     public HttpResponseMessage SaveDepartment(ClientDepartment depart)

    The class has the following properties. 

          public long Id { get; set; }
            public LighthouseProject.DalTypes.Enums.DepartmentTitles Type { get; set; }
            public long ParentId { get; set; }
            public string Name { get; set; }
            public string VatNum { get; set; }
            public string BusRegNum { get; set; }
            public long AddressId { get; set; }
            public long BrandId { get; set; }
            public System.DateTime CreatedOn { get; set; }
            public string CreatedBy { get; set; }
            public System.DateTime LastEditedOn { get; set; }
            public string LastEditedBy { get; set; }
            public LighthouseProject.DalTypes.Enums.RecordStatus Status { get; set; }
        
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<BrandAssetType> BrandAssetTypes { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<ClientDepartmentHouse> ClientDepartmentHouses { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<ClientDepartmentHouse> ClientDepartmentHouses1 { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<ContractorsToDepartment> ContractorsToDepartments { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<DepartmentAsset> DepartmentAssets { get; set; }
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<KeyContact> KeyContacts { get; set; }
            public virtual Address Address { get; set; }

    I just can't see where I am going wrong. Any help would be really appreciated!

    Best regards, 

         Seamus

    Saturday, October 24, 2015 10:14 AM

Answers

  • User-341994687 posted

    Hi, 

    If any one else has the same problem, it is that the name of the parameter is not included in the post request. So the request is like below. 

     var promise = $http.post(webApiUrls.hostUrl + 'ClientDepartments/SaveDepartment', department)

    Just don't add the name of the parameter in the method. 

    Best regards, 

         Seamus 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 27, 2015 8:09 AM

All replies