I'm trying to post form serialized values to controller (Web API Self Host).
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);
}
}
}
Many thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…