繁体   English   中英

Ajax 序列化表单的值未正确传递给我的控制器操作

[英]Ajax serialized form with values not getting passed correctly to my controller's action

我设置了一个 javascript 警报来确认我的 form.serialize 工作正常,而且确实如此。 问题是我的控制器操作没有将表单中的任何数据与模型相关联。

带有传递的表单值的 Javascript 警报

此警报中的第一个值是StartDate ,值为02/25/2020 ,但在下面的控制器屏幕截图中,它没有绑定到 dailyReport 对象的 StartDate 值。

警报

控制器和传递对象的屏幕截图

VS截图

阿贾克斯

$(function () {
    $('#submit').on('click', function (evt) {
        alert($('#reportForm').serialize());
        evt.preventDefault();
        //Ajax form post
        $.ajax({
            type: 'POST',
            data: $('#reportForm').serialize(),
            url: '@Url.Action("Daily","Reports")',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                if (data.success) {
                    alert("Data Success");
                } else {
                    console.log(data);
                    //Toggle the error modal and display error messages
                    $('#errorsModal').modal('toggle');
                    //Add <br> tags when there is a linebreak in the string.  This will add the line breaks into the HTML.
                    $('#errorsModal .modal-body p').html(data.message.replace(/(?:\r\n|\r|\n)/g, '<br>'));
                }
            }
        });
    });
});

控制器

[HttpPost]
public IActionResult Daily(Daily dailyReport)
{
    var dr = new ReportDaily();
    var rc = new ReportDailyCriteria();
    dr.Preview(rc, IntPtr.Zero, out Notification notification);
    if (notification.HasErrors)
    {
        return Json(new
        {
            success = false,
            message = notification.GetConcatenatedErrorMessage(Environment.NewLine + Environment.NewLine)
        });
    }

    return Json(new { success = true });
}

从使用'application/json; charset=utf-8'切换'application/json; charset=utf-8' 'application/json; charset=utf-8''application/x-www-form-urlencoded; charset=utf-8' fixed the issue. 'application/x-www-form-urlencoded; charset=utf-8' fixed the issue.

暂无
暂无

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

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