簡體   English   中英

發送消息 <t> 通過ajax轉換為復雜模型

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

我知道我之前已經做過,但是似乎無法使它正常工作。

我有以下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);
                }
            });
        });

在我的模型中,我有;

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

在我的控制器中,我有;

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

但是,當我發帖時,收件人列表始終為空。

如果不對接收方列表進行字符串化處理,則會得到相同的結果。

有人知道我在做什么錯嗎?

編輯其他值全部通過ok,只是列表為空。

您不需要JSON.stringify收件人。

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

在這里刪除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'}]
        };

試試這個,它應該工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM