我正在通過選中複選框來發送多個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函數中?
你嘗試'JSON.stringify({bulkEmailSendViewModel:EVAL(jsonObj)})'。也考慮做出Post請求。 – User3250
通過將json作爲一個字符串而不是一個對象數組來構建你的硬盤。 – James
打開Chrome,按f12,進入網絡選項卡,點擊保存日誌。現在,當SendBulkEmail $ .get發送時,發送的頭是什麼?它有效的JSON? https://jsonlint.com/ – phpmeh