Ben
Ben

Reputation: 431

FromBody Item Coming Back as Null

I am using JSGrid to allow me to modify data. I am trying to setup the updateItem as follows:

updateItem: function (item) {
                return $.ajax({
                    type: "PUT",
                    url: "/api/data/" + item.logID,
                    data: item,
                    contentType: "application/json;charset=utf-8",
                    dataType: "json"
                });
            }

I have a model as follows:

public class LogEntry
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string comment { get; set; }
    public bool modified { get; set; }
    public DateTime inTime { get; set; }
    public DateTime outTime { get; set; }
    public double totalHrs { get; set; }
    public int logID { get; set; }
}

Here is my ApiController Class that contains the Put method:

public class DataController : ApiController
{
    public void Put(int id, [FromBody]LogEntry item)
    {
        if(item != null)
        {
           //DO STUFF
        }

    }
}

However no matter what I do the item is always Null. I have tried the following:

I have used Fiddler to see the data sent back. It shows the correct JSON being sent but the Put method doesn't seem to be getting that data.

Any ideas on what might be causing this behavior would be great. I have been researching around but nothing has worked yet.

Resolution Notes

It turned out that both answers from Caz1224 and CodeFuller aided in fixing this. CodeFuller's way of finding the error message lead me to the JSON coming back to the server. It was messed up even though Fiddler said it was correct. I changed my Ajax call to stringify the item. Which then matched my Model exactly.

return $.ajax({
                    type: "PUT",
                    url: "/api/data/" + item.logID,
                    data: JSON.stringify(item),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"

Upvotes: 0

Views: 515

Answers (2)

CodeFuller
CodeFuller

Reputation: 31322

FromBody attribute is not strictly required here because non-simple types are built by the default from the request body.

I don't see any problems with your code so far, it should work for valid request. Seems like the problem is with the request sent and it causes model binding error. To proceed with the analysis check the following:

  1. Check the value of ActionContext.ModelState.IsValid in Put() method. Is it true or false?
  2. If it's false, check collection ActionContext.ModelState.Values in debugger. It should contain model binding errors that will hint you were the problem actually happens.

Upvotes: 1

Caz1224
Caz1224

Reputation: 1569

I had this issue and its painful!!

This is what ended up working for me. The first part is my javascript (I guess that is how you are making your JSON)

"List" is my array that PERFECTLY matches (Even to the case, it matters) to my C# model.

Javascript Build List

var List = [];
$.each(_questions, function () {
    var limitID = this.id.substr(-1);
    var Quest = $('#Quest_' + ID).val();
    var objectpush = {
        LimitID: limitID,
        Quest: Quest,
        ID: ID
    }
    List.push(objectpush);

Then in the AJAX call further on I specify data like so:

data: JSON.stringify(List),

And finally this is on my C# API

public JsonResult QueryLimits(int UserID, [FromBody] List<List> List)

Hope this helps get you on the track. Don't just copy the code as I changed the name to List throughout my code and that will cause issues!

Caz

Upvotes: 0

Related Questions