2017-05-26 110 views
0

我正在通過選中複選框來發送多個email/SMS。而當我在我的JavaScript函數中接收數據時。但是當我把它傳遞給行動方法記錄計數顯示,但所有的數據都是空的。下面是我的代碼截圖here控制器中的JSON數據爲空

這是我的模型:

public class BulkEmailSendViewModel 
    { 
     public BulkEmailSendViewModel() 
     { 
      Candidates = new List<CandidateData>(); 
     } 
     public List<CandidateData> Candidates { get; set; } 
     public string Body { get; set; } 
     public string Subject { get; set; } 

    } 
    public class CandidateData 
    { 
     public string Email { get; set; } 
     public string CandidateId { get; set; } 
     public string Phone { get; set; } 
     public string CandidateName { get; internal set; } 
    } 

//選取所有選中的複選框

$("#bulkAction").change(function() { 
     var ddlId = $("#bulkAction").val();//to get sms or email 
     var chk_arr = $('.checkCandidate:checkbox:checked'); 
     var chklength = chk_arr.length; 
     var json = ''; 
     $('.checkCandidate:checkbox:checked').each(function() { 
      if (this.checked) { 
       var Phone = $(this).attr("candidatePhone"); 
       var CandidateId = $(this).attr("candidateId"); 
       var Email = $(this).attr("candidatEmail"); 
       var item = '{\"Phone\":\"' + Phone + '\","CandidateId\":\"' + CandidateId + '\",\"Email\":\"' + Email + '\",\"CandidateName\":\"\"},'; 
       json += item; 
      } 
     }); 
     json = "[" + json.substr(0, json.length - 1) + "]"; 
     SendBulkEmail(json); 

    }); 

我的javascript:

function SendBulkEmail(jsonObj) { 
    alert(jsonObj); 
    if (jsonObj.length > 0) { 
     var send = "/Utility/Notifications/BulkEmail"; 
     $(".modal-title").text("Send Email"); 
     //var data = { 
     // Candidates: eval(jsonObj) 
     //}; 
     $.get(send, { bulkEmailSendViewModel: eval(jsonObj) }, function (result) { 
      $("#C_modal_body").html(""); 
      $("#C_modal_body").html(result); 
     }); 
    } 
    else { 
     $.alert("Email not found for this candidate."); 
     // e.stopPropagation(); 
    } 

} 

我的控制器:

public PartialViewResult BulkEmail(List<CandidateData> bulkEmailSendViewModel) 
     { 
      BulkEmailSendViewModel bulkDetail = new BulkEmailSendViewModel(); 
      return PartialView(bulkDetail); 
     } 

爲什麼我的所有值都爲空即使我在javascript函數中?

+1

你嘗試'JSON.stringify({bulkEmailSendViewModel:EVAL(jsonObj)})'。也考慮做出Post請求。 – User3250

+1

通過將json作爲一個字符串而不是一個對象數組來構建你的硬盤。 – James

+0

打開Chrome,按f12,進入網絡選項卡,點擊保存日誌。現在,當SendBulkEmail $ .get發送時,發送的頭是什麼?它有效的JSON? https://jsonlint.com/ – phpmeh

回答

0

改變你的JavaScript代碼如下:

$("#bulkAction").change(function() { 
    var ddlId = $("#bulkAction").val();//to get sms or email 
    var chk_arr = $('.checkCandidate:checkbox:checked'); 
    var chklength = chk_arr.length; 
    var data = []; 
    $('.checkCandidate:checkbox:checked').each(function() { 
     if (this.checked) { 
      var Phone = $(this).attr("candidatePhone"); 
      var CandidateId = $(this).attr("candidateId"); 
      var Email = $(this).attr("candidatEmail"); 
      var item = {Phone: Phone, 
         CandidateId: CandidateId, 
         Email : Email, 
         CandidateName : ""}; 
      data.push(item); 
     } 
    }); 
    SendBulkEmail(data); 
}); 

SendBulkEmail到:

function SendBulkEmail(data) { 
if (data.length > 0) { 
    var send = "/Utility/Notifications/BulkEmail"; 
    $(".modal-title").text("Send Email"); 
    //var data = { 
    // Candidates: eval(jsonObj) 
    //}; 
    $.post(send, { bulkEmailSendViewModel: JSON.stringify(data) }, function (result) { 
     $("#C_modal_body").html(""); 
     $("#C_modal_body").html(result); 
    }); 
} 
else { 
    $.alert("Email not found for this candidate."); 
    // e.stopPropagation(); 
} 
} 

最後:

[HttpPost] 
public PartialViewResult BulkEmail(List<CandidateData> bulkEmailSendViewModel) 
     { 
      BulkEmailSendViewModel bulkDetail = new BulkEmailSendViewModel(); 
      return PartialView(bulkDetail); 
     } 
+0

感謝它的工作,但不與json .stringify。它與eval –

+0

合作如果您有答案,請將答案標記爲正確。 –

0

在你的控制,我看不出有什麼使用bulkEmailSendViewModel輸入參數。

也許,你可以在候選列表中傳播如下:

public PartialViewResult BulkEmail(List<CandidateData> candidates) 
{ 
    BulkEmailSendViewModel bulkDetail=new BulkEmailSendViewModel(); 
    bulkDetail.candidates = candidates; 
    return PartialView(bulkDetail); 
}