【发布时间】:2015-12-07 01:11:59
【问题描述】:
我知道在堆栈溢出时已多次询问过此问题,但我遇到了一些奇怪的事情,想了解如何将其更改为正常工作。
我读到的所有内容都说,当将对象传递回 MVC 时,请确保您的 JSON 参数名称与 C# 的名称匹配,并确保 MVC 对象模型相同。当我尝试示例 2 时,我得到控制器中对象的空值,但如果我使用示例 1,我将获得所需的数据。
我想使用示例 2,我猜测问题的发生是因为与 WebApiConfig 或 global.asax 有关,希望有人能澄清一下。
型号
public class SearchCredential
{
public string Employee { get; set; }
public string CostCenter { get; set; }
}
控制器
[HttpPost]
public dynamic SearchEmployees(SearchCredential searchCriteria)
{
// some code goes here
}
Javascript 示例 1:
vm.searchCriteria = {"employee": vm.searchEmployee,
"costCenter": vm.searchCostCenter
};
$http.post(baseURL + 'SearchEmployees', vm.searchCriteria )
Javascript 示例 2:
vm.searchCriteria = {"employee": vm.searchEmployee,
"costCenter": vm.searchCostCenter
};
$http.post(baseURL + 'SearchEmployees', { searchCriteria: vm.searchCriteria })
【问题讨论】:
-
示例 2 中的代码应该可以正常工作。您运行的内容与您在此处发布的内容不同吗?
-
是的。只要名称与您的操作方法帕尔马名称匹配,它就应该起作用
-
感谢大家抽出宝贵时间查看我的问题。我发现示例 2 不起作用
标签: javascript c# angularjs json asp.net-mvc