如何将多个参数从javascript函数传递给控制器

时间:2021-05-01 16:40:48

I know that we can send multiple parameters by specifying them "," separated but what if count of parameters is dynamic.Below code works fine for specific amount of parameters. How can I accomplish sending of multiple parameters whose count can be varied ?

我知道我们可以通过指定它们来分别发送多个参数,但是如果参数的计数是动态的,那么该怎么办。下面的代码适用于特定数量的参数。如何完成多个参数的发送,这些参数的计数可以变化?

 $.ajax({
            type: "POST",
            url: 'Home/xxx',             
            data: { "FirstName": FirstName, "LastName": LastName,"InputNo3": InputNo3, "InputNo4": InputNo4, "InputNo5": InputNo5 }
        }).done(function (msg) {
            $(".popup").html(msg);
            $(".background").show();
        });

2 个解决方案

#1


0  

In case if parameters are dynamic, and you don't know what are they, then you can use a dictionary as Model. Here is a sample on Scott's blog - http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

如果参数是动态的,并且您不知道它们是什么,那么您可以使用字典作为模型。以下是Scott博客上的一个示例 - http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Backend code:

public ActionResult Blah(IDictionary<string, string> parameters) {
    if (parameters.ContainsKey("FirstName"))
    {
        var firstNameValue = parameters["FirstName"];
        // do something with it
    }
}

JavasScipt code:

var objData = [];
objData[0].Key = "FirstName";
objData[0].Value = FirstName;
objData[1].Key = "LastName";
objData[1].Value = LastName;
objData[2].Key = "InputNo3";
objData[2].Value = InputNo3Value;
// populate other needed properties


$.ajax({
    type: "POST",
    url: 'Home/xxx',             
    data: objData
}).done(function (msg) {
    $(".popup").html(msg);
    $(".background").show();
});

#2


0  

If you have many parameters with different type like string, int, object, better you can add it into object and send it.

如果您有许多不同类型的参数,如string,int,object,那么您可以将其添加到对象中并发送它。

var myComplextObject={};
myComplextObject.FirstName='';
myComplextObject.Age=27;
myComplextObject.Address={};
myComplextObject.Address.City='Chennai';

//etc
$.ajax({
        type: "POST",
        url: 'Home/xxx',             
        data: myComplextObject
    }).done(function (msg) {
        $(".popup").html(msg);
        $(".background").show();
    });

#1


0  

In case if parameters are dynamic, and you don't know what are they, then you can use a dictionary as Model. Here is a sample on Scott's blog - http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

如果参数是动态的,并且您不知道它们是什么,那么您可以使用字典作为模型。以下是Scott博客上的一个示例 - http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Backend code:

public ActionResult Blah(IDictionary<string, string> parameters) {
    if (parameters.ContainsKey("FirstName"))
    {
        var firstNameValue = parameters["FirstName"];
        // do something with it
    }
}

JavasScipt code:

var objData = [];
objData[0].Key = "FirstName";
objData[0].Value = FirstName;
objData[1].Key = "LastName";
objData[1].Value = LastName;
objData[2].Key = "InputNo3";
objData[2].Value = InputNo3Value;
// populate other needed properties


$.ajax({
    type: "POST",
    url: 'Home/xxx',             
    data: objData
}).done(function (msg) {
    $(".popup").html(msg);
    $(".background").show();
});

#2


0  

If you have many parameters with different type like string, int, object, better you can add it into object and send it.

如果您有许多不同类型的参数,如string,int,object,那么您可以将其添加到对象中并发送它。

var myComplextObject={};
myComplextObject.FirstName='';
myComplextObject.Age=27;
myComplextObject.Address={};
myComplextObject.Address.City='Chennai';

//etc
$.ajax({
        type: "POST",
        url: 'Home/xxx',             
        data: myComplextObject
    }).done(function (msg) {
        $(".popup").html(msg);
        $(".background").show();
    });