0

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


        });
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
LouisPL
  • 55
  • 1
  • 1
  • 12
  • array insted of new '{ Val1 = Val1 , columnNames = columnNames}'? – Govind Malviya Jun 26 '12 at 05:20
  • instead of messy querystring why not push items to javascript array and post to server? http://stackoverflow.com/questions/1290131/javascript-how-to-create-an-array-of-object-literals-in-a-loop and http://stackoverflow.com/questions/7116099/send-array-to-mvc-controller-via-json – Adam Tuliper Jun 26 '12 at 05:30
  • Thanks for all of your quick response.. @Govind.. I want to pass Val1 and ColumnName[string] array to Server.. My expression is incorrect.. pls show me How to pass to these values to Server via javascript. Thanks. – LouisPL Jun 26 '12 at 05:53
  • with ajax, URL not change to New View ... My want is currently on ://Localhost/search/OrganizationSearch and I want to go ://localhost/search/OrgDataExport . so I use Url.Action method.. to go New URL.. – LouisPL Jun 26 '12 at 07:45

3 Answers3

1

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 columnNames as a string array

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.

$('#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();
});
VJAI
  • 32,167
  • 23
  • 102
  • 164
  • Hi @Mark.. thank for your code sample.. It work for me..only i need to edit abit.."Index?" + $.param({ Val1 : Val1 , columnNames : columnNames }); Pls let me know one thing, can i hide my parameter from url. bcos now in url, my parameter is too long.. any suggestion? waiting ur reply, – LouisPL Jun 26 '12 at 09:54
  • That's a real problem. I would suggest you to submit the values to the sever through a HTML form using POST. – VJAI Jun 26 '12 at 09:56
  • can u show me any sample codes or pls refer me any post . this is my first exp... thanks in advance.. – LouisPL Jun 26 '12 at 10:01
  • I updated the answer. If you already have a form in your view utilize that else create a dynamic one as I did above. – VJAI Jun 26 '12 at 10:37
  • Hi Mark. Thanks for your code.it work. Let me know one thing, Can i return View() with new window? pls suggest me something.. – LouisPL Jun 27 '12 at 09:23
  • Do you want to open the view returned in the form post in new window? – VJAI Jun 27 '12 at 09:48
  • Thanks for quick reply @Mark..Now I got the ans..by adding "target: "_blank"," to form attribute. in your code.. Cheers.!. – LouisPL Jun 27 '12 at 10:22
0
 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.

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
}
Community
  • 1
  • 1
alok_dida
  • 1,723
  • 2
  • 17
  • 36
  • @alok_dida.."columnNames" var is just string. i query from jquery gird selected columns. when i printout it, it got the value like "col1,col2,col3,col4". so i want to send this string to controller . now i testing as u suggest . with ajax. thanks. – LouisPL Jun 26 '12 at 07:23
  • with ajax, URL not change to New View ... My want is currently on ://Localhost/search/OrganizationSearch and I want to go ://localhost/search/OrgDataExport . so I use Url.Action method.. to go New URL.. – LouisPL Jun 26 '12 at 07:44
  • @Louismm . You can use directly use window.location = '://localhost/search/OrgDataExport?Val1='+val1+'&columnNames='+columnNames; on your export button click event. Please have a look http://www.tizag.com/javascriptT/javascriptredirect.php – alok_dida Jun 26 '12 at 09:49
0

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.

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

$('#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:

$Val1 = $this->_request->getparam("Val1");
David 'the bald ginger'
  • 1,296
  • 3
  • 20
  • 38
  • 2
    He also tagged his question with `asp.net-mvc-3`, not `php` or `zend`. In addition to that `'Val1' => Val1` is invalid javascript. – Darin Dimitrov Jun 26 '12 at 06:29
  • Perfectly correct @DarinDimitrov, thanks for pointing that out (upvoted it). I am aware Louis hadn't tagged php or Zend, but that is just the implementation of MVC for the JQuery-Ajax related answer I gave. :) – David 'the bald ginger' Jun 26 '12 at 07:04
  • @logansama.. Thanks for ur reply.. I want to use javascript code inside .cshtml to Controller.cs in MVC3. – LouisPL Jun 26 '12 at 07:25