ASP.NET Web API 2 with Angular woes

Hi there

This question relates to a problem I'm having with my (first) Web API 2 project. One of the actions on my controller is being hit successfully, but the values of the parameter passed in are all in their default states and do not correspond to the values sent from the Angular behaviour.

My controller looks like this:

[HttpPost]
public IEnumerable<string> Post(SearchParameters id)
{
    return null;
}

public struct SearchParameters
{
    string brokerIsUnallocated;
    string brokerIncludeDeleted;
    string businessType;
    bool codeC;
    bool codeD;
    bool codeP;
    bool codeS;
    bool codeT;
    bool codeX;
    string companyName;
    string contactName;
    string country;
    string customerId;
    string department;
    string selectedBroker;
    string town;
}

Open in new window


My Web API 2 controller is successfully being hit, but the view model passed as a parameter is not being populated. It contains nulls for strings and falses for booleans.

My AngularJS call looks like this:

        $scope.search = function () {
            var id = { id : {
                        selectedBroker: typeof ($scope.selectedBroker) === 'undefined' ? 0 : $scope.selectedBroker.userIdField,
                        brokerIsUnallocated: $scope.brokerIsUnallocated || false,
                        brokerIncludeDeleted: $scope.brokerIncludeDeleted || false,
                        customerId: $scope.customerCustomerId || 0,
                        businessType: $scope.customerBusinessType || "",
                        companyName: $scope.customerCompanyName || "",
                        town: $scope.customerTown || "",
                        department: $scope.selectedDepartment || 0,
                        contactName: $scope.customerContactName || "",
                        country: $scope.selectedCountry || 0,
                        codeP: $scope.codeP || false,
                        codeC: $scope.codeC || false,
                        codeT: $scope.codeT || false,
                        codeS: $scope.codeS || false,
                        codeX: $scope.codeX || false,
                        codeD: $scope.codeD || false
                    }
                }

            $http({
                method: 'POST',
                url: '/api/customer',
                data: id
            });
        }

Open in new window


Fiddler looks like this:

Fiddler capture
I am using the default routing, like this:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Open in new window


Can anyone suggest what's going on?

Looking forward to your responses. Mark
mark_norgateAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Try changing your controller method to:

[HttpPost]
public IEnumerable<string> Post([FromBody]SearchParameters id)
...

Open in new window

mark_norgateAuthor Commented:
I did try that today, but it had no effect.

Mark
käµfm³d 👽Commented:
You also need to remove "id: { .. }" wrapper you have in the Javascript. Just use an anonymous object.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mark_norgateAuthor Commented:
Ok thanks, I'll give that a shot when I get back to work on Monday.

M
mark_norgateAuthor Commented:
Yep, that fixed it. Thank you very much!

Mark
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.