简体   繁体   中英

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;

        $("#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.

You don't need to JSON.stringify the recipients.

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

Remove JSON.stringify form here and it should work.

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 = "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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