I'm trying to post form serialized values to controller.
I cannot understand why the NameValueCollection is not correctly bound.
Client-side using jQuery:
// Form Submit Handler
$( '#form-parameters' ).submit(function (event) {
event.preventDefault();
var formData = $(this).serialize();
// Post serialized form data
postAssemblyParameters(formData);
});
// Post Form Data to controller test
function postAssemblyParameters(formData){
$.ajax({
url: http://localhost/api/test/1,
type: 'POST',
data: formData,
dataType: 'application/x-www-form-urlencoded',
success: function(x3d) {
},
error: function(xhr) {
}
});
}
Server-side using Web API Self Host:
public void Post([FromUri] int id, [FromBody] NameValueCollection formData)
{
Console.WriteLine(id); // OK
// Collection is NULL
foreach (var key in formData.AllKeys)
{
foreach (var val in formData.GetValues(key))
{
Console.WriteLine(key + ": " + val);
}
}
}
Jumpa
Member
1 Points
9 Posts
POST Form Data Collection
Nov 28, 2012 10:56 AM|LINK
I'm trying to post form serialized values to controller.
I cannot understand why the NameValueCollection is not correctly bound.
Client-side using jQuery:
// Form Submit Handler $( '#form-parameters' ).submit(function (event) { event.preventDefault(); var formData = $(this).serialize(); // Post serialized form data postAssemblyParameters(formData); }); // Post Form Data to controller test function postAssemblyParameters(formData){ $.ajax({ url: http://localhost/api/test/1, type: 'POST', data: formData, dataType: 'application/x-www-form-urlencoded', success: function(x3d) { }, error: function(xhr) { } }); }Server-side using Web API Self Host:
public void Post([FromUri] int id, [FromBody] NameValueCollection formData) { Console.WriteLine(id); // OK // Collection is NULL foreach (var key in formData.AllKeys) { foreach (var val in formData.GetValues(key)) { Console.WriteLine(key + ": " + val); } } }Anyone providing a working example? Many thanks.
Careed
All-Star
18764 Points
3637 Posts
Re: POST Form Data Collection
Nov 28, 2012 12:36 PM|LINK
Try using FormCollection in place of NameValueCollection.
"The oxen are slow, but the earth is patient."
Jumpa
Member
1 Points
9 Posts
Re: POST Form Data Collection
Nov 28, 2012 04:21 PM|LINK
Thanks!
More specifically for Self Host I'm using FormDataCollection.