简体   繁体   中英

cannot pass parameters from jquery ajax to code behind c#

I am facing an issue that when i am passing testbox values through jquery ajax i cannot see values in code behind.

function CreateComplaint(){

 var category       = $('#ddlCompCategory') .val();
 var name           = $('#txtCompUserName') .val();
 var subject        = $('#txtCompSub')      .val();
 var mobile         = $('#txtCompMobileNo') .val();
 var address        = $('#txtaCompAddr')    .val();
 var email          = $('#txtCompEmail')    .val();


$('#spinnerAddComplaint').show();
 CreateComplaintXHR(
                     '/api/Complaints/CreateComplaint', 
                     {
                        Token       : RBApp.Token,
                        CategoryId  : category,
                        UName       : name,
                        Subject     : subject,
                        Mobile      : mobile,
                        Email       : email,
                        Address     : address
                     }, ....}

ajax function:

function CreateComplaintXHR(url, data, success, error, alWaysCallback) {
  $.ajax({
      url: url,
      type: 'POST',
      dataType: 'json',
      data: data,
      success: function (data, textStatus, jqXHR) {

          if (success)
              success(data, textStatus, jqXHR);
      },
      error: function (jqXHR, textStatus, errorThrown) {

          if (error)
              error(jqXHR, textStatus, errorThrown);
      }
  }).always(function () {

  });

  }

code behind:

 [HttpPost]
    [ActionName ( "CreateComplaint" )]
    public ResponseModel InsertComplaintMethod(InsertComplaint Complaint)
    {
        log .Debug ( "Create Complaint request received : " );
        log .Debug ( "Request params : " + Complaint .Serialize() );

        User user = loadUser ( Complaint .Token );

        ComplaintsAdapter adapter = new ComplaintsAdapter ( user );

        ResponseModel response = adapter .CreateComplaintOrSuggestion ( Complaint );



        return response;
    }

InsertComplaint

public class InsertComplaint
{
    //public string Token { get; set; }
    public string Lat = "0"; //{ get; set; }
    public string Lng = "0"; //{ get; set; }

    public string deviceID = "0";
    public string complaintSubject { get; set; }
    public string complaintText { get; set; }

    public string name    { get; set; }
    public string phoneNumber { get; set; }
    public string attachements = null;


    }

In insertComplaintMethod function object complaint is not getting any values instead its showing null. is this the issue because in ajax function data has less parameters than in complaint object which has more properties. anyone can help. thanks in advance.

Names here

{
                        Token       : 'test',
                        CategoryId  : 'test',
                        UName       : 'test',
                        Subject     : 'test',
                        Mobile      : 'test',
                        Email       : 'test',
                        Address     : 'test'
}

and here

public class InsertComplaint
{
    //public string Token { get; set; }
    public string Lat = "0"; //{ get; set; }
    public string Lng = "0"; //{ get; set; }
    public string deviceID = "0";
    public string complaintSubject { get; set; }
    public string complaintText { get; set; }
    public string name    { get; set; }
    public string phoneNumber { get; set; }
    public string attachements = null;
}

should be the same. For example replace this line

 Mobile : 'test',

with this one

 phoneNumber : '123'

and you will see '123', not null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM