javascript Url.Action 在 MVC 中将字符串数组从视图传递到控制器#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11200637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:22:33  来源:igfitidea点击:

Url.Action Passing string array from View to Controller in MVC#

javascriptarraysasp.net-mvc-3jqueryurl-routing

提问by LouisPL

I have a method that returns an array (string[]) and I'm trying to pass this array of strings into an Action.Currently I can't pass my parameters. I am new in MVC3. Pls let me know why I can't pass parameter to ActionResult..I already define ActionResult with Same parameter name.. thanks all in advance....

我有一个返回数组 ( string[])的方法,我正在尝试将此字符串数组传递给 Action。目前我无法传递我的参数。我是 MVC3 的新手。请让我知道为什么我不能将参数传递给 ActionResult ..我已经定义了具有相同参数名称的 ActionResult .. 提前感谢...

$('#export-button').click(function () {

            var columnLength = $("#grid")[0].p.colNames.length;
            var columnNames = "";
            for (var i = 0; i < columnLength; i++) {
                if ($("#grid")[0].p.colModel[i].hidden == false) {
                    columnNames = columnNames + $("#grid")[0].p.colModel[i].name + ',';
                }
            }
            var Val1 = jQuery(txt_search1).val();
            alert(Val1); alert(columnNames);
            document.location = '@Url.Action("OrgDataExport","Search", new { Val1 = Val1 , columnNames = columnNames})';


        });

回答by VJAI

Try this,

试试这个,

$('#export-button').click(function () {

    var columnLength = $("#grid")[0].p.colNames.length;

    // columnNames is an object now
    var columnNames = {};

    for (var i = 0; i < columnLength; i++) {
        if ($("#grid")[0].p.colModel[i].hidden == false) {
            columnNames[i] = $("#grid")[0].p.colModel[i].name;
        }
    }

    var Val1 = jQuery(txt_search1).val();

    document.location = "Home/Index/" + $.param({ Val1 = Val1 , columnNames = columnNames });
});

Your action that takes columnNamesas a string array

columnNames作为字符串数组的操作

public ActionResult Index(string val1, string[] columnNames)
{
// Your code
}

UPDATE:

更新:

If the URL becomes too big you can submit the values through form using POST method. If your view already have a form use that else create a dynamic one on the fly and submit the values through POST.

如果 URL 变得太大,您可以使用 POST 方法通过表单提交值。如果您的视图已经有一个表单,请使用它动态创建一个动态表单并通过 POST 提交值。

$('#export-button').click(function () {

    var Val1 = jQuery(txt_search1).val();    

    $("#hidden-form").remove();

    // create a form dynamically
    var form = $('<form>')
            .attr({ id: "hidden-form",
              action: "/Home/Index",
              method: "post",
              style: "display: none;"
            })
            .appendTo("body");            

    // add the "Val1" as hidden field to the form.
    $('<input>').attr({ name: "Val1 ", value: Val1, type: "hidden" }).appendTo(form);

    var columnLength = $("#grid")[0].p.colNames.length;

    // add the "columnNames" as hidden fields to the form
    for (var i = 0; i < columnLength; i++) {
        if ($("#grid")[0].p.colModel[i].hidden == false) {
            var t = $("#grid")[0].p.colModel[i].name;
            $('<input>').attr({ name: "columnNames", value: t, type: "hidden"
             }).appendTo(form);
        }
    };

    // submit the form
    form.submit();
});

回答by alok_dida

 for (var i = 0; i < columnLength; i++) {
                if ($("#grid")[0].p.colModel[i].hidden == false) {
                    columnNames = columnNames + $("#grid")[0].p.colModel[i].name + ',';
                }
            }
            var Val1 = jQuery(txt_search1).val();
            alert(Val1); alert(columnNames);
            document.location = '@Url.Action("OrgDataExport","Search", new { Val1 = Val1 , columnNames = columnNames})';

Hi Louis,

嗨,路易斯,

Your are trying to access javascript varaibles Val1 and columnNames from the server side tag and it is not possible. For more details, please refer this URL.

您正在尝试从服务器端标记访问 javascript 变量 Val1 和 columnNames,但这是不可能的。有关详细信息,请参阅此URL

You can do it by following way.

您可以通过以下方式进行。

    var jsonData = { val1 : Val1, columnNames : columnNames };

$.ajax({
          type: "GET", //GET or POST or PUT or DELETE verb
                url: "Home/Index", // Location of the service
                data: jsonData,
                contentType: "application/json; charset=utf-8", // content type sent to server
                processdata: true, //True or False
                success: function () {
                  alert("success")
                }
       });

On your controller side you have to write like

在您的控制器方面,您必须像这样写

public ActionResult Index(string val1, string columnNames)
{
// Your code
}

回答by David 'the bald ginger'

You tagged JQuery-Ajax but i don't see any ajax attempt in the code example? So i am guessing you want to know an Ajax orientated solution. You're probably not using Zend Framework, but i hope this answers helps point you in the right direction to a solution.

您标记了 JQuery-Ajax,但我在代码示例中没有看到任何 ajax 尝试?所以我猜你想知道一个面向 Ajax 的解决方案。您可能没有使用 Zend Framework,但我希望这些答案有助于为您指明解决方案的正确方向。

From JS/Zend framework experience you could look at something like

根据 JS/Zend 框架经验,您可以查看类似的内容

$('#export-button').click(function () {
   ....
   var actionUrl= "/controller/action/";
   $.ajax({
      url: actionUrl,
      data: {
        variable1: "OrgDataExport",
        variable2: "Search",
        Val1: Val1,
        columnNames: columnNames            
      },
      dataType: "json",
      success: function(json) {
        //do stuff
      }
   });
   ....
});

In the ZendFramework controller you can then grab the variables on the request:

在 ZendFramework 控制器中,您可以获取请求中的变量:

$Val1 = $this->_request->getparam("Val1");