1

Ok, this might be simple, I'm having a simple $.post call to server sending string array as parameters..

 $.get('/home/ReadCalPacTagValue', data, function (data) {
            data = $.parseJSON(data);
            if (data.length != 0) {
                var ReadFromDb = data[0]["PushToDb"].replace('PushToDb','ReadFromDb');
                var DBAckno = ReadFromDb.replace('ReadFromDb', 'DataAck');
                var FIdTag = ReadFromDb.replace('ReadFromDb', 'FluidTypeId');
                var UserIdTag = ReadFromDb.replace('ReadFromDb', 'UserId');
                var UniqueIdTag = ReadFromDb.replace('ReadFromDb', 'UniqueRecordId');
                var dbconnTag = ReadFromDb.replace('ReadFromDb', 'DatabaseConnectionString');
                updateTags = [dbconnTag,FIdTag,ReadFromDb, UserIdTag,UniqueIdTag];
                actionvalue = ["", fluidtypeid, '1', userid, uniqueID];
                var data_Tags = { updateTags: updateTags, actionvalue: actionvalue }
                $.post('/home/WriteCalPacTagValue', data_Tags, function (response) {
                    //var Path = "Config/16_CalPac/" + FluidType + "/" + metername + "/" + FileName
                    //$.cookie('FileName', FileName, { expires: 7, path: '/' });
                    //$.cookie('FilePath', Path, { expires: 7, path: '/' });
                    //$.cookie('ModuleName', "CalPac", { expires: 7, path: '/' });
                    //window.open('../home/CalPac', '_blank');
                });
            } else {
                    swal("Error !", "Data operation tag not binded for this product", "warning");
            }
        })

my problem is, every time it makes $.post call, server is getting null values int prarameters..

 public void WriteCalPacTagValue(string[] updateTags, string[] actionValue)
        {
            string[] writetags = { };
            DanpacUIRepository objNewTag = new DanpacUIRepository();

            if (updateTags.Count() > 0)
            {
                actionValue[0] = ConfigurationManager.AppSettings["DBString"].ToString();
                for (int i = 0; i < updateTags.Count(); i++)
                {

                    writetags = updateTags[i].Replace("&lt;", "").Replace("&gt;", ">").Split('>');
                    objNewTag.WriteTag(writetags, actionValue[i]);
                }

            }
        }

I'm not getting what I've done wrong here.. whereas same function is working from another JS file with some difference string into array updateTags.

any help?

Abhinav
  • 1,202
  • 1
  • 8
  • 12
  • 1
    Your sending arrays of data so you need to use the `$.ajax()` method so that you can set `contentType: 'application/json',` and use stringify the data - `data: JSON.stringify(data_Tags),` . Alternatively you must send individual collection items - `{ updateTags[0]: dbconnTag, updateTags[1]:'FIdTag, .... }` –  Jun 16 '17 at 13:54
  • 1
    Try with `ajax` and `traditional: true` - https://stackoverflow.com/questions/11868051/jquery-post-with-serialize-and-array-of-data – Dan Dumitru Jun 16 '17 at 13:58
  • Thanks This solved problem when I explicitly set ajaxsetting traditional:true.. – Abhinav Jun 16 '17 at 14:15

1 Answers1

1

Having

public class DataTags
{
    public string[] UpdateTags { get; set; }
    public string[] ActionValue { get; set; }
}

At the server: Change the method to this

[HttpPost()]
public void WriteCalPacTagValue([FromBody]DataTags data_Tags)
{

}

At the client: call it

$.ajax({
    type: 'POST',
    url: '/home/WriteCalPacTagValue',
    data: data_Tags,
    success: function (response) { 
    //your code
    }
});

Also you can send the whole data as json string using data: JSON.stringify(data_Tags), in javascript code the change the WriteCalPacTagValue to accept a single string at the parameter and deserialize it in C# code at the server side.

EDIT if you cannot change the server side code, you may follow this as stated in the comments.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116