MVC中的AJAX POST数据行始终为null

时间:2018-03-21 21:55:06

标签: json ajax asp.net-mvc null

任何人都可以帮助我以下内容。我正在尝试将JSON数据发送到控制器,但控制器从不用数据填充变量。

这是控制器并始终显示为null。

这是控制器。

public ActionResult SyncMail(List<MailSyncDTO> emailLst)
{
    return null;
}

视图看起来像这样;

foreach (var item in Model)
    <td><input name="emailLst.emailSource" id="emailSource" type="text" value="@item.emailSource" /></td>
    <td><input name="emailLst.emailDestination" id="emailDestination" type="text" value="@item.emailDestination" /></td>
    <td><input name="emailLst.SyncMail" id="SyncMail" type="checkbox" /></td>
    <td><input name="emailLst.ID" id="ID" type="text" value="@item.ID" hidden /></td>
</tr>
}

    function Update() {
        var emailLst = [];
        $('#tblEmails tr').each(function () {
            var obj = new Object();
            var emailSource = $(this).find("input").each(function () {
                if (this.id == 'emailSource' && this.value != '') {
                    obj.emailSource = this.value;
                }
                else if (this.id == 'emailDestination' && this.value != '') {
                    obj.emailDestination = this.value;
                }
                else if (this.id == 'SyncMail' && this.value != '') {
                    obj.SyncMail = this.value;
                }
                else if (this.id == 'ID' && this.value != '') {
                    obj.ID = this.value;
                }
            })
            emailLst.push(obj);
        });

        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: '/MailSyncDBs/SyncMail',
            data: JSON.stringify({ emailLst: emailLst })
        });
    }

我可以在Fiddler中看到数据正在发送。

POST http://localhost:53545/MailSyncDBs/SyncMail HTTP/1.1
Content-Type: application/json; charset=utf-8
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Referer: http://localhost:53545/MailSyncDBs/Index
Accept-Language: en-GB
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Length: 336
Host: localhost:53545
Connection: Keep-Alive
Pragma: no-cache
{"emailLst":[{},{"emailSource":"source@mail.co.uk","emailDestination":"destination@mail.co.uk","SyncMail":"on","ID":"1"},{"emailSource":"source1@mail.co.uk","emailDestination":"destination1@mail.co.uk","SyncMail":"on","ID":"2"},{"emailSource":"source2@mail.co.uk","emailDestination":"destination2@mail.co.uk","SyncMail":"on","ID":"3"}]}

这是引用的DTO;

public class MailSyncDTO
{
    public string ID { get; set; }
    public string emailSource { get; set; }
    public string emailDestination { get; set; }
    public string SyncMail { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您的控制器希望收到List<string>,但您不会向他发送一个。在你制作控制器的方式中,你发送的Json应该是这样的:

["source@co.uk","source@co.uk","source@co.uk"]

您可以在C#项目中创建一个DTO,它将完全反映您的Json,并将在您的控制器中自动映射。

我已经使用这种javascript方法对其进行了测试:

function testIt() {
    var emailLst = [];

    // Replace bellow part with yours
    var obj = new Object();
    obj.EmailDestination = "dest@co.uk";
    obj.Source = "source@Context.uk";
    obj.SyncMail = "on";
    obj.Id = "1";
    emailLst.push(obj);
    // Replace above part with yours


    // Adding this before sending will make sure that you are not sending any empty objects.
    emailLst.forEach(function(item, i) {         
        if ($.isEmptyObject(item)) {
            emailLst.splice(i, 1);
        }
    });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/MailSyncDBs/SyncMail',
        data: JSON.stringify(emailLst)  // And not JSON.stringify({emailLst : emailLst })
    });
}

尝试添加类似这样的类:

public class SyncMailDto
{
    public string EmailSource {get; set;}
    public string EmailDestination {get; set;}
    public string SyncMail {get; set;}
    public string Id {get; set;}    
}

并将控制器方法的签名更改为:

[HttpPost]
public JsonResult SyncMail(List<SyncMailDto> emailLst){

    foreach(var mail in emailLst){
         // Do something with your mail
    }

    return Json("It Works !");
}

如果你想完全使用你编写的javascript,你必须添加另一个DTO类来包装列表。因为那是你用JSON.stringify({EmailLst : EmailLst})做的。

public class MailListWrapperDto{
    public List<SyncMailDto> EmailLst {get;set;}
}

你的POST方法签名:

public JsonResult SyncMail(MailListWrapperDto emailLst)