Reputation: 2855
I have the following controller action using the web api on a .net 4.0 web forms web application.
public dynamic Post(Guid id, string adminNotes = "", string artNotes = "")
{
dynamic response = new ExpandoObject();
response.Success = false;
Udac udac = CustomerUdacs.Where(x => x.EditKey == id).FirstOrDefault();
if (udac != null)
{
udac.AdminNotes = adminNotes.Trim();
udac.ArtNotes = artNotes.Trim();
response.Success = true;
}
return response;
}
I'm using jQuery ajax() to post the data through. I am using a Guid as my id (which is stored in ajax_edit_key in my javascript). When I use this:
$.ajax({
type: "POST",
url: "/api/notes/"+ajax_edit_key,
data: { adminNotes: $("#tbEditNotesAdmin").val(), artNotes: $("#tbEditNotesArt").val() }
}).done(function (data) {
if (data.Success) {
$("#tbEditNotesAdmin").val(''); //td.id-notes-admin
$("#tbEditNotesArt").val(''); //td.id-notes-art
ajax_edit_key = '';
$dialog.dialog("close");
}
}).fail(function () {
alert('fail');
});
it calls the action but my adminNotes and artNotes are passing through from the browser as empty strings.
If I change the url to include the parameters in the querystring it works fine.
url: "/api/notes/"+ajax_edit_key+"?"+ "adminNotes="+$('#tbEditNotesAdmin').val()+"&artNotes="+$('#tbEditNotesArt').val()
//data: { adminNotes: $("#tbEditNotesAdmin").val(), artNotes: $("#tbEditNotesArt").val() }
Why is this? How can I get it to use the data parameter
Upvotes: 2
Views: 1608
Reputation: 3770
The problem here is the way how WebAPI engine parses multiple parameters. It can parse only one parameter, which comes from body request by default, and you have two.
The solution would be:
For the second option you need simply to create the model:
public class NotesModel
{
public string AdminNotes { get; set; }
public string ArtNotes { get; set; }
}
And change signature of your action to:
public dynamic Post(Guid id, NotesModel model)
And you don't need to change your JS.
Upvotes: 2