I know this has been asked many times on stack overflow but I ran into something weird and want to understand how I can change it to work right.
Everything I read said that when passing back an object to MVC make sure your JSON parameter name matches that of your C# and make sure the MVC object model is the same. When I try example 2 I get null values for the object in the controller but if I use example 1 I get the data as desired.
I would like to use example 2 and I am guessing that the problem happens because of something to do with the WebApiConfig or the global.asax and am hoping someone can clarify.
Model
public class SearchCredential
{
public string Employee { get; set; }
public string CostCenter { get; set; }
}
Controller
[HttpPost]
public dynamic SearchEmployees(SearchCredential searchCriteria)
{
// some code goes here
}
Javascript example 1:
vm.searchCriteria = {"employee": vm.searchEmployee,
"costCenter": vm.searchCostCenter
};
$http.post(baseURL + 'SearchEmployees', vm.searchCriteria )
Javascript example 2:
vm.searchCriteria = {"employee": vm.searchEmployee,
"costCenter": vm.searchCostCenter
};
$http.post(baseURL + 'SearchEmployees', { searchCriteria: vm.searchCriteria })