0

I have the collection of string and array variable which is need to pass controller through ajax post action. for Ex:

string str="john";
int temp= 10;
var arrayObj=new Array();
$.ajax({

           type: 'Post',
           url: 'home/controller',
           data: ?, // how to pass all string,int and array object
           datatype: ?, //'html/ or Json' what will need to give?
          success: function (result) {
    }, 
});

Anyone could answer my questions? Thanks, Bharathi.

Bharathi
  • 1,288
  • 2
  • 14
  • 40
  • Which one is it… JavaScript or C#? Are you looking for help on the client or server side part of this? And do you want it to/does it serve HTML, or JSON? – Ry- Nov 14 '13 at 05:24
  • 1) what does the receiving controller method look like? 2) dataType is the data that jQuery will receive. If you get a partial view back, for example, that should be 'html'. 3) contentType is how you specify what data type the posting data is. – ps2goat Nov 14 '13 at 05:27
  • See my answer here http://stackoverflow.com/questions/19956913/optimal-way-to-forward-data-selected-from-an-actionresult-to-another/19957860?noredirect=1#comment29706828_19957860 – Matt Bodily Nov 14 '13 at 05:31
  • I need to get this ajax request in controller. server side. My question is, 1. Can we pass the js Array object in ajax 'html' datatype method with string or int collection. 2. Or. should we use JSON to pass the js Array object to server. I need the exact JavaScript code to pass this collections in ajax post – Bharathi Nov 14 '13 at 05:35

1 Answers1

0

Without seeing your controller method:

var str="john";
var temp= 10;
var arrayObj = [somevar, somevar2];

$.ajax({

           type: 'Post',
           url: 'home/controller',
           data: {nameParameter: str, tempParameter: temp, arrayParameter: arrayObj},               
           contentType: 'text',
          /* dataType:  , depends on what your controller method returns */
          success: function (result) {
    }
});

The other option is to use JSON2 library (NuGet).

'data' would now use

JSON.stringify({ nameParameter: str, tempParameter: temp, arrayParameter: arrayObj })

The contentType should then change to application/json

ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • Thanks, but, I already stored some double value in the array index arrayObj[0]=1.4, arrayObj[1]= 3.6, like that. i need to send string, int and array object collection. – Bharathi Nov 14 '13 at 05:44
  • You can also omit the `contentType` and `dataType` parameters until you have things working. jQuery can usually infer the types for you. – ps2goat Nov 14 '13 at 05:49