我的网络项目中有一个课程:
public class MyClass
{
public int? Param1 { get; set; }
public int? Param2 { get; set; }
}
这是我的控制器方法中的一个参数:
public ActionResult TheControllerMethod(MyClass myParam)
{
//etc.
}
如果我使用POST调用方法,则模型绑定会自动工作(我在js端使用angular,这可能没关系):
$http({
method: "post",
url: controllerRoot + "TheControllerMethod",
data: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
如果我使用GET,则该参数在控制器中始终为null。
$http({
method: "get",
url: controllerRoot + "TheControllerMethod",
params: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
使用默认模型绑定器的复杂模型绑定是否仅适用于POST,或者我可以做些什么使它与GET一起使用?
答案是肯定的。GET和POST请求之间的区别在于POST正文可以具有内容类型,因此可以在服务器端将它们正确解释为XML或Json,等等。对于GET,您所拥有的只是一个查询字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句