I'm trying to using FormCollection instead of a model. I created my AJAX request:
myData = {
id: '1',
name: $("#name").val()
};
$.ajax({
type: "GET",
url: '/testController/Test',
data: JSON.stringify(myData),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// do something
},
error: function (erro) {
console.debug(erro);
}
});
My controller with FormCollection
[HttpGet, ValidateInput(false)]
public JsonResult Test(FormCollection formData)
{
string name = formData["name"].ToString();
}
However, my formData is always null. Does anyone know why?
Thanks
If you are making GET request, you are actually making this request. And you need to access the parameter by Request['id']
& Request['name']
.
Request URI: /testController/Test?id={data_id}&name={data_name}
Request Body: <empty>
However, if you are making POST request, you are making this request:
Request URI: /testController/Test
Request Body: {"id":"data_id","name":"data_name"}
The request body is parsed and you can access the data through FormCollection
. That's why FormCollection
is always null when you make GET request.