0

I have read a lot of questions regarding similar issues, but none of them seems to work for me.

I am receiving null for the movie object in the post action method.

This is my controller:

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

This is the Movies class:

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()
    {

    }
}

This is the post request (using postman), I have tried either application/json and application/json; charset=utf-8 as the Content-Type but in both cases I have received null for the movie object:

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

Postman screenshots: Body

Headers

How can I fix this?

4
  • I tested your code with Postman, no problems with the code, hence that narrows down the issue to usage of Postman. Do you put your request JSON into view named "Body" in Postman? BR Commented Jun 27, 2021 at 11:29
  • 1
    Yes, I put into body, and picked the "raw" option Commented Jun 27, 2021 at 11:31
  • OK, can you then add screen-shots to your question for the following request settings from Postman: Headers, Body Commented Jun 27, 2021 at 11:37
  • 1
    I added them to the question Commented Jun 27, 2021 at 11:41

3 Answers 3

1

Please try to change your request JSON to this:

{
    "title":"Some Movie",
    "rating":1.87,
    "year":2011
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Your model must first be serialized and then passed to data in ajax call. this is one sample, You have to change the model according to your needs....

$("#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);  
                }  
            });  
        }  
    });

Comments

0

in the postman you can use different way to call method. Click on code button and choose one of way to call method.

enter image description here

you can use jquery and put code onto section....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.