【问题标题】:JSON data null in controller控制器中的 JSON 数据为空
【发布时间】:2017-05-26 14:30:28
【问题描述】:

我正在通过选中复选框发送多个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 函数?

【问题讨论】:

  • 你试过JSON.stringify({ bulkEmailSendViewModel: eval(jsonObj) })。还可以考虑使其发布请求。
  • 通过将 json 构建为字符串而不是对象数组,您正在以艰难的方式做到这一点。
  • 打开Chrome,按f12,进入网络标签,点击保留日志。现在,当 SendBulkEmail $.get 发送时,发送的标头是什么?它是有效的 JSON 吗? jsonlint.com
  • 旁注:“发布”请求在语义上更适合于此。你正在做的(发送)与 get 完全相反。另外,为什么要将 JSON 构建为字符串,然后使用“eval”?只需向对象添加属性(JSON is 一个 javascript 对象 - 名称中的线索),您就可以更轻松地做到这一点,而不会让自己面临语法和编码错误或固有的安全风险使用 eval。
  • 我在这里看不到的唯一其他事情是您的[FromBody] 控制器功能上的[FromBody] 标记 - public PartialViewResult BulkEmail([FromBody]List&lt;CandidateData&gt; bulkEmailSendViewModel)

标签: javascript jquery json asp.net-mvc


【解决方案1】:

将您的 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);
        }

【讨论】:

  • 感谢它有效,但不适用于 json.stringify。它与 eval 一起使用
  • 如果您得到答案,请将答案标记为正确。
【解决方案2】:

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

也许,您可以按如下方式传播候选列表:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2021-10-08
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多