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.
}
If I call the method using POST, the model binding works automatically (I use angular on the js side, which likely doesn't matter):
如果我使用POST调用方法,模型绑定会自动生效(我在js端使用angular,这可能无关紧要):
$http({
method: "post",
url: controllerRoot + "TheControllerMethod",
data: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
If I use a GET, the parameter is always null in the controller.
如果我使用GET,则控制器中的参数始终为null。
$http({
method: "get",
url: controllerRoot + "TheControllerMethod",
params: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
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?
使用默认模型绑定器的复杂模型绑定是否仅适用于POST,或者我可以做些什么来使其与GET一起工作?
3 个解决方案
#1
10
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和POST请求之间的区别在于POST主体可以具有内容类型,因此可以在服务器端将它们正确解释为XML或Json,依此类推;对于GET,你所拥有的只是一个查询字符串。
#2
8
With ASP.NET MVC you can indeed bind your model on a GET request, as long as you have the same query string parameter names as of the property names of your Model class. Example from this answer:
使用ASP.NET MVC,您确实可以在GET请求上绑定模型,只要您具有与Model类的属性名称相同的查询字符串参数名称。这个答案的例子:
public class ViewModel
{
public string Name { set;get;}
public string Loc{ set;get;}
}
You can do a Get request like this
你可以这样做一个Get请求
MyAction?Name=jon&Loc=America
and MVC will automatically bind your model:
和MVC将自动绑定您的模型:
[HttpGet]
public ViewResult MyAction(ViewModel model)
{
// Do stuff
return View("ViewName", model);
}
#3
-2
Why are you calling the property "data" in the POST, and "params" in the GET? Both should be called "data".
你为什么要在POST中调用属性“data”,在GET中调用“params”?两者都应该被称为“数据”。
$http({
method: "get",
url: controllerRoot + "TheControllerMethod",
data: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});
#1
10
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和POST请求之间的区别在于POST主体可以具有内容类型,因此可以在服务器端将它们正确解释为XML或Json,依此类推;对于GET,你所拥有的只是一个查询字符串。
#2
8
With ASP.NET MVC you can indeed bind your model on a GET request, as long as you have the same query string parameter names as of the property names of your Model class. Example from this answer:
使用ASP.NET MVC,您确实可以在GET请求上绑定模型,只要您具有与Model类的属性名称相同的查询字符串参数名称。这个答案的例子:
public class ViewModel
{
public string Name { set;get;}
public string Loc{ set;get;}
}
You can do a Get request like this
你可以这样做一个Get请求
MyAction?Name=jon&Loc=America
and MVC will automatically bind your model:
和MVC将自动绑定您的模型:
[HttpGet]
public ViewResult MyAction(ViewModel model)
{
// Do stuff
return View("ViewName", model);
}
#3
-2
Why are you calling the property "data" in the POST, and "params" in the GET? Both should be called "data".
你为什么要在POST中调用属性“data”,在GET中调用“params”?两者都应该被称为“数据”。
$http({
method: "get",
url: controllerRoot + "TheControllerMethod",
data: {
myParam: myParam
}
}).success(function (data) {
callback(data);
}).error(function () {
alert("Error getting my stuff.");
});