0

This is a development of a question that I asked earlier that changed into a different problem.

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 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;
}

I have checked in Fiddler and every parameter is being passed. Do I somehow need to pass this as a parameter called ID to match the controller action? 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 }
    );
}

Fiddler request looks like this:

Fiddler capture

Looking forward to your responses.

Community
  • 1
  • 1
serlingpa
  • 12,024
  • 24
  • 80
  • 130
  • I would guess, from your last question, that your data isn't in the correct JSON format – Jonesopolis Apr 10 '15 at 12:15
  • It looks like the same issue with: http://stackoverflow.com/questions/21618471/web-api-put-is-recognizing-query-strings-but-not-body/21618545#21618545 . Check out my answer in the question – Khanh TO Apr 11 '15 at 03:02

1 Answers1

0

Change your definition to:

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

The notation FromBody should serialize your data.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
netrevisanto
  • 1,091
  • 12
  • 13
  • Your return type also should be : IHttpActionResult, not IEnumerable. – netrevisanto Apr 10 '15 at 12:17
  • I have implemented your responses serially and together but now my controller action is not being hit at all. I am getting a 404. – serlingpa Apr 10 '15 at 12:21
  • What your route definition data look like? Paste it in your question, please. – netrevisanto Apr 10 '15 at 12:24
  • Route definitions added above. I think the route is working because the controller action was being hit when I had my original method signature. – serlingpa Apr 10 '15 at 12:28
  • Well, nothing bad, i presume. Can we see that "Fiddler" you mention in the question? – netrevisanto Apr 10 '15 at 12:34
  • I have added Fiddler's request. It's not a particularly useful capture because I didn't add any parameters to the search, but I assure you that even with a completely populated view model the action has null values in the parameter. – serlingpa Apr 10 '15 at 12:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74933/discussion-between-serlingpa-and-netrevisanto). – serlingpa Apr 10 '15 at 12:39