0

I tried to pass more than one parameter from client-side using jQuery to ASP Web API method, but the method can't accept that. I tried some of the solutions but the same thing.

Web API:

[HttpPost]
[ResponseType(typeof(Message))]
[Route("api/Messages/send-message")]
public async Task<IHttpActionResult> SendMessage(Email email, Message message){} 

jQuery:

   $.ajax({
       url: '/api/Messages/send-message',
       method: 'POST',
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       data: JSON.stringify({
            email: EmailsArray,
            title: $('#txtTitle').val(),
            body: $('#txtContent').val(),
         }),
       success: function (response) {
           console.log(response);
        });

Error Message:

"message":"An error has occurred.","exceptionMessage":"Can't bind multiple parameters ('email' and 'message') to the request's content.","exceptionType":"System.InvalidOperationException"
3
  • 2
    Create a model class that has properties of types Email and Message then send that one object. Commented Apr 18, 2019 at 11:42
  • Also in your JS you are currently sending three parameters (email, title, and body) not two. "message" is nowhere to be seen in that data object. But yes just wrap them all up in a DTO...see answer below Commented Apr 18, 2019 at 11:45
  • Natively WebAPI doesn't support binding of multiple POST parameters Commented Apr 18, 2019 at 11:53

2 Answers 2

4

If you're sending the data as JSON then all the data needs to be contained in a single coherent JSON structure. Having two separate input parameters on the server side does not fit with this concept.

In this situation you can create a DTO (Data Transfer Object) which is a class holding all of the items you want to transfer. Something like this:

public class EmailMessageDTO
{
  public Email email { get; set; }
  public Message message { get; set; }
}

Then you define the action method as accepting this single over-arching object

public async Task<IHttpActionResult> SendMessage(EmailMessageDTO dto) { } 

And in the JavaScript:

data: JSON.stringify({
  email: EmailsArray,
  message: { 
    "title": $('#txtTitle').val(),
    "body": $('#txtContent').val(),
  }
}),

It's quite similar to the concept of having a ViewModel in MVC.

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

Comments

1

You need to add [FromBody] attribute to parameter and you need to make post data to application/x-www-form-urlencoded and you send only email parameter, you need to add message parameter if you wanna send title and body you need to create model like model:

public class EmailSendModel(){
      public string email{get;set;}
      public string title{get;set;}
      public string body{get;set;}
}

controller:

[HttpPost]
[ResponseType(typeof(Message))]
[Route("api/Messages/send-message")]
public async Task<IHttpActionResult> SendMessage([FromBody]EmailSendModel model){}

3 Comments

"you need to make post data to application/x-www-form-urlencoded" ...this is not strictly necessary, no. Web API will accept either that or JSON.
And FromBody should not be required when the input parameter is a non-primitive type. (I think in ASP.NET Core you maybe have to set that, but this question is tagged as ASP.NET traditional)
I tried [FromBody] before, but didn't work. Now I tried the @ADyson solution above, and it works well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.