简体   繁体   English

从视图到 controller 的 Ajax 请求正文在 ASP NET Core MVC 3 中为空

[英]Ajax request body from view to controller is empty in ASP NET Core MVC 3

I have a scenario where I want to make an ajax call from view to controller in ASP NET Core MVC 3. The controller should then return partial view as a response.我有一个场景,我想在 ASP NET Core MVC 3 中从视图对 controller 进行 ajax 调用。然后 controller 应该返回部分视图作为响应。 The main problem is that when data gets passed to controller via ajax post request, controller finds data null.主要问题是,当数据通过 ajax 发布请求传递到 controller 时,controller 找到数据 Z207B9Z776CC08CDAFFAE I've searched through a lot of online content on this topic, but no solution works for me.我已经搜索了很多关于这个主题的在线内容,但没有适合我的解决方案。

Here is my controller method:这是我的 controller 方法:

[HttpPost]
public PartialViewResult AddUserForm(string firstName, string lastName) 
{
    // some stuff happens then
    return PartialView();
}

My Ajax post method in view:我的 Ajax 后视图中的方法:

<script>
    function replaceContentsOfDiv() {
        var f = $('#fName').val();
        var l = $('#lName').val();

        $.ajax({
            url: '@Url.Action("AddUserForm", "Reservation")',
            contentType: 'application/json',
            data: JSON.stringify({ firstName: this.f, lastName: this.l }),
            type: "POST",
            success: function(data) {
                $('#placeHolderDiv').html(data);
            },
            error: function() {
                alert("something is wrong");
            }
        });
    }
</script>

The data from this js script ( var f and var l have valid values. However, if I want to log out string firstName and string lastName in controller method, they are both null. This can be tested with I. e. logger.LogInformation((firstName == null).ToString()) .此 js 脚本中的数据( var fvar l具有有效值。但是,如果我想在 controller 方法中注销string firstNamestring lastName ,它们都是 null。这可以用logger.LogInformation((firstName == null).ToString())

Anyone knows what I'm doing wrong so that the data that gets passed into controller is null?任何人都知道我做错了什么,因此传递到 controller 的数据是 null? I even tried adding [FromBody] annotations to AddUserForm parameters, but no success either.我什至尝试将[FromBody]注释添加到AddUserForm参数,但也没有成功。

EDIT: fixed some typo in my JS script.编辑:修复了我的 JS 脚本中的一些错字。

You just need to change your ajax like following:你只需要改变你的 ajax 如下:

$.ajax({
        url: '@Url.Action("AddUserForm", "Reservation")',
        data: {
                 firstname: f,
                 lastname: l
                },
        type: "POST",
        success: function(data) {
            $('#placeHolderDiv').html(data);
        },
        error: function() {
            alert("something is wrong");
        }
    });

I then found the solution.然后我找到了解决方案。 The problem was how the request was called.问题是如何调用请求。 I then just used regular jQuery post method like this:然后我只使用了常规的 jQuery 发布方法,如下所示:

<script>
    function replaceContentsOfDiv() {
        let data = {
            firstName: document.getElementById("fName").value,
            lastName: document.getElementById("lName").value
        }
        console.log(data);
        $.post('@Url.Action("AddUserForm", "Reservation")', data, function (data) {
            $('#placeHolderDiv').html(data);
        });
    };
</script>

暂无
暂无

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

相关问题 如何从ASP.NET MVC控制器和AJAX中的请求URI和正文中读取信息? - How to read both from request URI and body in ASP.NET MVC controller and AJAX? 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC 在asp.net core 2 MVC中通过ajax在视图中显示来自控制器的json字符串 - displaying json strings from controller in view over ajax in asp.net core 2 MVC ASP.NET Core MVC - 数据没有从视图传递到 controller 在 post 请求中 - ASP.NET Core MVC - Data does not pass from the view to the controller in a post request ASP NET Core (MVC) 将参数从视图传递到控制器的问题 - ASP NET Core (MVC) problem with passing parameters from the view to the controller 来自.Net Core 3.0的请求正文为空 - Request body from is empty in .Net Core 3.0 使用asp.net mvc 5操作控制器按名称读取ajax请求主体值 - reading ajax request body values by name with asp.net mvc 5 action controller c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view 向ASP.NET MVC Core中的控制器发出AJAX请求时,参数为null - Param is null when making AJAX Request to controller in ASP.NET MVC Core ASP.NET Core MVC Ajax发布到具有多个参数的控制器返回错误的请求 - Asp.net core mvc ajax post to controller with multiple parameters return bad request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM