简体   繁体   English

无法将参数从 jquery ajax 传递到 C# 后面的代码

[英]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.我面临一个问题,当我通过 jquery ajax 传递测试盒值时,我看不到后面代码中的值。

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.在 insertComplaintMethod 函数中,对象投诉没有得到任何值,而是显示为空。 is this the issue because in ajax function data has less parameters than in complaint object which has more properties.这是问题吗,因为在 ajax 函数中数据的参数少于具有更多属性的投诉对象。 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.你会看到'123',而不是空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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