【问题标题】:Post action in ASP.NET MVC always receives nullASP.NET MVC 中的发布操作始终接收 null
【发布时间】:2021-06-27 10:36:30
【问题描述】:

我已经阅读了很多关于类似问题的问题,但似乎没有一个对我有用。

我收到 null 用于 post 操作方法中的电影对象。

这是我的控制器:

public class MovieController : Controller
{
    [Route("movies/all")]
    [HttpGet] 
    public ActionResult All()
    {
        List<string> movies = new List<string>();
        movies.Add("Some Movie");
        movies.Add("Diamond Team");

        //Get movies
        return Ok(movies);
    }

    [Route("movies/post")]
    [HttpPost] 
    public ActionResult Post([FromBody] Movie movie)
    {
        Console.WriteLine(movie);
        List<string> movies = new List<string>();
        movies.Add(movie.title);

        //Get movies
        return Ok(movies);
    }

    public ActionResult Index()
    {
        return View();
    }
}

这是电影类:

public class Movie
{  
    public string title { get; set; }
    public float rating { get; set; }
    public int year { get; set; }

    public Movie(string title,float rating, int year)
    {
        this.title = title;
        this.rating = rating;
        this.year = year;
    }

    public Movie()
    {

    }
}

这是发布请求(使用邮递员),我尝试将 application/jsonapplication/json; charset=utf-8 作为 Content-Type,但在这两种情况下我都收到了电影对象的 null:

{
    "title":"Some Movie",
    "rating":"1.87",
    "year":"2011"
}

邮递员截图:

我该如何解决这个问题?

【问题讨论】:

  • 我用 Postman 测试了您的代码,代码没有问题,因此将问题缩小到 Postman 的使用范围。您是否将请求 JSON 放入 Postman 中名为“Body”的视图中? BR
  • 是的,我放入了body,并选择了“raw”选项
  • 好的,您能否为来自 Postman 的以下请求设置的问题添加屏幕截图:标题、正文
  • 我将它们添加到问题中

标签: c# asp.net asp.net-mvc


【解决方案1】:

请尝试将您的请求 JSON 更改为:

{
    "title":"Some Movie",
    "rating":1.87,
    "year":2011
 }

【讨论】:

    【解决方案2】:

    您的模型必须首先被序列化,然后在 ajax 调用中传递给数据。这是一个示例,您必须根据需要更改模型....

    $("#btnPost").click(function() {  
            var employee = new Object();  
            employee.Name = $('#txtName').val();  
            employee.Address = $('#txtDesignation').val();  
            employee.Location = $('#txtLocation').val();  
            if (employee != null) {  
                $.ajax({  
                    type: "POST",  
                    url: "/JQueryAjaxCall/AjaxPostCall",  
                    data: JSON.stringify(employee),  
                    contentType: "application/json; charset=utf-8",  
                    dataType: "json",  
                    success: function(response) {  
                        if (response != null) {  
                            alert("Name : " + response.Name + ", Designation : " + response.Designation + ", Location :" + response.Location);  
                        } else {  
                            alert("Something went wrong");  
                        }  
                    },  
                    failure: function(response) {  
                        alert(response.responseText);  
                    },  
                    error: function(response) {  
                        alert(response.responseText);  
                    }  
                });  
            }  
        });
    

    【讨论】:

      【解决方案3】:

      在邮递员中你可以使用不同的方式来调用方法。单击代码按钮并选择一种调用方法的方式。

      您可以使用 jquery 并将代码放到部分中......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        相关资源
        最近更新 更多