使用GET的ASP.Net MVC模型绑定复杂对象 [英] ASP.Net MVC Model Binding Complex Object using GET
问题描述
我的网络项目中有一个课程:
I have a class in my web project:
public class MyClass
{
public int? Param1 { get; set; }
public int? Param2 { get; set; }
}
这是我控制器方法中的一个参数:
which is a parameter in my controller method:
public ActionResult TheControllerMethod(MyClass myParam)
{
//etc.
}
如果我使用POST调用方法,模型绑定会自动运行(我使用在js方面的角度,这可能无关紧要):
If I call the method using POST, the model binding works automatically (I use angular on the js side, which likely doesn't matter):
$http({
method: "post",
url: controllerRoot + "TheControllerMethod",
data: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
如果我使用GET,控制器中的参数始终为空。
If I use a GET, the parameter is always null in the controller.
$http({
method: "get",
url: controllerRoot + "TheControllerMethod",
params: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
使用默认模型绑定器的复杂模型绑定是否仅适用于POST,或者我可以做些什么使用GET进行此操作?
Does complex model binding using the default model binder only work for POSTs, or is there something I can do to make this work with a GET?
推荐答案
答案是肯定的。 GET和POST请求之间的区别在于POST主体可以具有内容类型,因此可以在服务器端将它们正确解释为XML或Json,依此类推;对于GET,你所拥有的只是一个查询字符串。
The answer is Yes. The difference between GET and POST requests is that a POST body can have a content type so they can be interpreted correctly on the server side as XML, or Json, so on; for GET, all you have is just a querystring.
这篇关于使用GET的ASP.Net MVC模型绑定复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!