如何在MVC3中使用javascript将参数发送到控制器方法?

时间:2012-09-13 14:58:50

标签: asp.net-mvc-3

如何将参数从javascript发送到控制器方法?例如,我正在尝试发送参数“2012”

以下是我视图中的脚本:

$.ajax({
                        type: "get", url: "test", data: {}, // How can i fill this data like @item ?
                        success: function (data) {

                            alert(data);

                        }

我的控制器方法:

public string test()
        {

            TestServices client = new TestServices();
            client.ClientCredentials.UserName.UserName = "service_test";
            client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            decimal? x = client.GetTest(2012, 7, 1);

            return (""+x);


        }

1 个答案:

答案 0 :(得分:3)

[HttpGet]    
public string test(string name, string age)

和ajax

$.ajax({
                    type: "get", url: "test", data: {name:"Messi",age:"36yo"}, // How can `i fill this data like @item ?`
                    success: function (data) {

                        alert(data);

                    }