Abhinav
Abhinav

Reputation: 1200

$.post always getting null values into server

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?

Upvotes: 1

Views: 153

Answers (1)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

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.

Upvotes: 1

Related Questions