控制器中的JSON数据为null

时间:2017-05-26 14:30:29

标签: javascript jquery json asp.net-mvc

我正在努力通过选中复选框发送多个email/SMS。当我在我的javascript函数中收到它时,它正在获取数据。但是当我将它传递给动作方法时,记录计数显示但所有数据都为空。以下是我的代码,其中包含屏幕截图here

这是我的模特:

 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函数,我的所有值都为null?

2 个答案:

答案 0 :(得分: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);
        }

答案 1 :(得分:0)

在您的控制器中,我没有看到使用bulkEmailSendViewModel输入参数。

也许,您可以将候选列表传播如下:

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