2017-02-13 44 views
1

我面臨的一個問題是,當我通過jquery ajax傳遞測試框值時,我無法在代碼後面看到值。無法將參數從jquery ajax傳遞給c後面的代碼#

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 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() { 

    }); 

    } 

代碼後面:

[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; 


    } 

在insertComplaintMethod功能對象的投訴沒有得到任何值而不是其舒g null。這是問題,因爲在Ajax函數中,數據的參數比具有更多屬性的投訴對象的參數少。任何人都可以幫忙 在此先感謝。

回答

1

名稱這裏

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

這裏

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; 
} 

應該是相同的。例如替換該行

Mobile : 'test', 

與這一個

phoneNumber : '123' 

,你會看到 '123',而不是空。

+0

謝謝你。我花了2天時間來找出問題所在。有用。再次感謝。 –

+0

歡迎您! – Pavlo

相關問題