We help IT Professionals succeed at work.
Get Started

ASP.NET Web API 2 with Angular woes

233 Views
Last Modified: 2015-04-13
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
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE