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


        });
4
  • array insted of new '{ Val1 = Val1 , columnNames = columnNames}'? Commented Jun 26, 2012 at 5:20
  • instead of messy querystring why not push items to javascript array and post to server? stackoverflow.com/questions/1290131/… and stackoverflow.com/questions/7116099/… Commented Jun 26, 2012 at 5: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. Commented Jun 26, 2012 at 5: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.. Commented Jun 26, 2012 at 7:45

3 Answers 3

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();
});
Sign up to request clarification or add additional context in comments.

7 Comments

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,
That's a real problem. I would suggest you to submit the values to the sever through a HTML form using POST.
can u show me any sample codes or pls refer me any post . this is my first exp... thanks in advance..
I updated the answer. If you already have a form in your view utilize that else create a dynamic one as I did above.
Hi Mark. Thanks for your code.it work. Let me know one thing, Can i return View() with new window? pls suggest me something..
|
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
}

3 Comments

@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.
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..
@Louismm . You can use directly use <Javascript Code Start> window.location = '://localhost/search/OrgDataExport?Val1='+val1+'&columnNames='+columnNames; <Javascript Code End> on your export button click event. Please have a look tizag.com/javascriptT/javascriptredirect.php
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");

3 Comments

He also tagged his question with asp.net-mvc-3, not php or zend. In addition to that 'Val1' => Val1 is invalid javascript.
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. :)
@logansama.. Thanks for ur reply.. I want to use javascript code inside .cshtml to Controller.cs in MVC3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.