schumacherj
schumacherj

Reputation: 1324

Value cannot be null. Deserialize Object Json

I am having issues deserializing this json string to a c# object. I have tried numerous different configuration of the model and i have tried to serialize the code and let the mvc value providers to do this, but i can't get it to work..... So I am sending this JSON string to my controller and then putting it into an object and then creating the correct object to throw it in my database.

[ArgumentNullException: Value cannot be null.
Parameter name: value]
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +162
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +66
   InSight.Controllers.QuestionController.CreateSimpleQuestion(String json) +25

This is the string before i send it to my controller:

var data = JSON.stringify({
            QuestionTitle: title,
            Keywords: key,
            Description: desc,
            Comments: comments,
            QuestionType: type,
            choices: {
                DisplayText: text,
                OrderNumber: order,
                is_correct:is_correct
            }
        });

This is the controller method:

public ActionResult CreateSimpleQuestion(string json)
    {
        SimpleQuestion temp = JsonConvert.DeserializeObject<SimpleQuestion>(json);
        Question question = new Question();
        question.QuestionTitle = temp.QuestionTitle;
        question.QuestionType = temp.QuestionType;
        question.Keywords = temp.Keywords;
        question.is_counted = true;
        question.DateCreated = DateTime.Now;
        question.Comments = temp.Comments;
        question.QuestionType = "Simple";
        db.Questions.Add(question);

        db.QuestionChoices.Add(temp.choices.First());
        db.SaveChanges();
        return RedirectToAction("Index");
    }

and this is the model:

public class SimpleQuestion
    {
            public int QuestionId { get; set; }

            public string QuestionTitle { get; set; }

            public DateTime DateCreated { get; set; }

            public string QuestionType { get; set; }

            public string Keywords { get; set; }

            public bool is_counted { get; set; }

            public string Description { get; set; }

            public string Comments { get; set; }

        public List<QuestionChoices> choices { get; set; }
    }

finally, this is the actual string of data that is getting passed:

{"QuestionTitle":"This is the Question Title",
"Keywords":"Blue pony, sharks",
"Description":"This is the description field.",
"Comments":"No comment has been left.",
"choices":{
    "DisplayText":"Will it rain tomorrow?",
    "OrderNumber":"1","is_correct":false
  }
} 

Solution Change the JS where data was defined to the following:

            var data = {
            "QuestionTitle": title,
            "Keywords": key,
            "Description": desc,
            "Comments": comments,
            "QuestionType": type,
            "choices": {
                "DisplayText": text,
                "OrderNumber": order,
                "is_correct":false
            }
        };

Upvotes: 1

Views: 14922

Answers (2)

Shyju
Shyju

Reputation: 218732

The real solution for your problem is to use MVC's model binding feature. Change your method parameter type to your class and MVC will bind the JSON value to it.

public ActionResult Create(SimpleQuestion model)
{
  // use model now
  // TO DO :Save and redirect
}

Make sure you are specifying the contentType property value as "application/json" in your ajax call.

Also you do not need to call stringify if it is a valid JSOn. The below code will work fine

$(function () {
    var data = {
        "QuestionTitle": "This is the Question Title",
        "Keywords": "Blue pony, sharks",
        "Description": "This is the description field.",
        "Comments": "No comment has been left.",
        "choices": {
            "DisplayText": "Will it rain tomorrow?",
            "OrderNumber": "1",
            "is_correct": false
        }
    };      
    $.post("@Url.Action("Create","Home")", data, null, 'application/json');

});

enter image description here

Upvotes: 3

user2674389
user2674389

Reputation: 1143

Your problem is - as so often when it comes to JSON - a mismatch between the JSON and your data structure.

In your data structure you have this: A list of QuestionChoices.

public List<QuestionChoices> choices { get; set; }

In your JSON you send this: A single object.

        choices: {
            DisplayText: text,
            OrderNumber: order,
            is_correct:is_correct
        }

Please keep in mind that in JSON an array (or a list) is described with []. So the correct JSON would be this:

        choices: [{
            DisplayText: text,
            OrderNumber: order,
            is_correct:is_correct
        }]

Multiple choices would be seperated within the [] by a comma.

The problem originates in your JavaScript code already, where you define choices to be a single object - not an array containing a single object. Fix that, and your problem should disappear.

Upvotes: 3

Related Questions