使用GET的ASP.Net MVC模型绑定复杂对象 [英] ASP.Net MVC Model Binding Complex Object using GET

查看:107
本文介绍了使用GET的ASP.Net MVC模型绑定复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络项目中有一个课程:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆