I have a JSON object:
{ "Questions": { "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }, "items": ["3a19f538-0cf6-e311-93f5-000c2948090b"]},
And i'm trying to send it to MVC controler action:
$.ajax({
type: "POST",
url: url,
data: { "Questions": { "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }, "items": ["3a19f538-0cf6-e311-93f5-000c2948090b"]},
dataType: 'json',
traditional: true,
}).done(AprovingResponse);
But in my action:
[POST]
public JsonResult AddFeedback(TestModel model)
{
[...]
}
Not whole model is filled:
public class TestModel
{
public List<Guid> items { get; set; }
public List<ViewModelQuestion> Questions { get; set; }
}
public class ViewModelQuestion
{
public Guid Id { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
}
Only item list is filled, Questions list contains 0 elements
enter image description here
I cannot find source of this problem. Can You help me?
--EDIT--
When i changed json to mach object collection, there is no diffrence:
data: { "Questions": [{ "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }, { "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }], "items": ["3a19f538-0cf6-e311-93f5-000c2948090b", "3a19f538-0cf6-e311-93f5-000c2948090b"] },
enter image description here
1 Answer 1
I think that your JSON object is not suitable to a class. I think rather should be:
{ "Questions": [{ "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }], "items": ["3a19f538-0cf6-e311-93f5-000c2948090b"]}
In your JSON Questions
property is single JSON object (with fields Id, Question, Answer), but in your view model you are expecting a collection of objects.
2 Comments
data: JSON.stringify({ "Questions": [{ "Id": "3a19f538-0cf6-e311-93f5-000c2948090b", "Question": "wedwe", "Answer": "4" }], "items": ["3a19f538-0cf6-e311-93f5-000c2948090b"]})
. I'm not sure if it will help, but I think it's worth of trying. There could be a problem with content-type in your post. Try to define it in a full way: contentType: 'application/json; charset=utf-8'
.