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.

2
  • I would guess, from your last question, that your data isn't in the correct JSON format Commented Apr 10, 2015 at 12:15
  • It looks like the same issue with: stackoverflow.com/questions/21618471/… . Check out my answer in the question Commented Apr 11, 2015 at 3:02

1 Answer 1

0

Change your definition to:

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

The notation FromBody should serialize your data.

Sign up to request clarification or add additional context in comments.

7 Comments

Your return type also should be : IHttpActionResult, not IEnumerable<string>.
I have implemented your responses serially and together but now my controller action is not being hit at all. I am getting a 404.
What your route definition data look like? Paste it in your question, please.
Route definitions added above. I think the route is working because the controller action was being hit when I had my original method signature.
Well, nothing bad, i presume. Can we see that "Fiddler" you mention in the question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.