中将字符串数组从View传递到Controller

中将字符串数组从View传递到Controller

本文介绍了Url.Action在MVC#中将字符串数组从View传递到Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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....

$('#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})';


        });

推荐答案

尝试一下,

$('#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 });
});

您将 columnNames 作为字符串数组的操作

Your action that takes columnNames as a string array

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

更新:

如果URL太大,则可以使用POST方法通过表单提交值.如果您的视图已具有某种形式的使用,则可以动态创建一个动态视图并通过POST提交值.

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.

$('#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();
});

这篇关于Url.Action在MVC#中将字符串数组从View传递到Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:11