简体   繁体   English

发送消息 <t> 通过ajax转换为复杂模型

[英]Sending LIst<t> via ajax to complex model

I know I've done this before but I can't seem to get this to work. 我知道我之前已经做过,但是似乎无法使它正常工作。

I have the following JavaScript; 我有以下JavaScript;

        $("#btnTestVouchers").click(function () {
            var postData = {
                "workplaceGiverId": $(".wpgDropdownList").val(),
                "fromMemberId": $(".wpgFromMemberDropdownList").val(),
                "toMemberId": $(".wpgToMemberDropdownList").val(),
                "voucherExpiryDate": $("#expiryDatePicker").val(),
                "recipients": JSON.stringify("[{'firstname':'a','lastname':'b','email':'c','voucheramount':'d'}]")
            };
            console.log(postData);
            $.ajax({
                type: "POST",
                url: "/Admin/TestVoucherCreationEmails",
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                data: JSON.stringify(postData),
                success: function (d) {
                    alert("OK");
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert("Error:" + errorThrown);
                }
            });
        });

In my model I have; 在我的模型中,我有;

public class postDataObject
{
    public int workplaceGiverId { get; set; }
    public int fromMemberId { get; set; }
    public int toMemberId { get; set; }
    public string voucherExpiryDate { get; set; }
    public IEnumerable<BulkVoucherRecipient> recipients { get; set; }
}

public class BulkVoucherRecipient
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string voucheramount { get; set; }
}

In my controller I have; 在我的控制器中,我有;

    [HttpPost]
    public void TestVoucherCreationEmails(postDataObject postedData)
    {
        string g = "";
    }

However when I post, the list of recipients is always empty. 但是,当我发帖时,收件人列表始终为空。

If I don't Stringify the list of recipients I get the same result. 如果不对接收方列表进行字符串化处理,则会得到相同的结果。

Anyone know what I'm doing wrong? 有人知道我在做什么错吗?

edit The other values all come through ok, just the List is empty. 编辑其他值全部通过ok,只是列表为空。

You don't need to JSON.stringify the recipients. 您不需要JSON.stringify收件人。

"recipients": JSON.stringify("[{'firstname':'a','lastname':'b','email':'c','voucheramount':'d'}]")

Remove JSON.stringify form here and it should work. 在这里删除JSON.stringify表单,它应该可以工作。

var postData = {
            "workplaceGiverId": $(".wpgDropdownList").val(),
            "fromMemberId": $(".wpgFromMemberDropdownList").val(),
            "toMemberId": $(".wpgToMemberDropdownList").val(),
            "voucherExpiryDate": $("#expiryDatePicker").val(),
            "recipients": [{'firstname':'a','lastname':'b','email':'c','voucheramount':'d'}]
        };

Try this, It should work 试试这个,它应该工作

[HttpPost]
public void TestVoucherCreationEmails([FromBody]postDataObject postedData)
{
    string g = "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM