Reputation: 1535
I am trying to get my model from inside my web app, into a post Action. the only issue is that I get a model object which has 1 null variables inside :(. The action is:
[HttpPost]
[ValidateAntiForgeryHeader]
public async Task<JsonResult> StartRound(RoundModel model)
the models are as follow:
Edit: thanks to Nick Bailey, I started to find heaps of issues edits to the following:
public class RoundModel
{
public List<ClientMatch> Matches { get; set; } // null in action
}
public class ClientMatch
{
public int OldId { get; set; }
public string RoundName { get; set; }
public string ServerName { get; set; }
public string ServerPassword { get; set; }
public string ServerMessage { get; set; }
public Guid? SystemId { get; set; }
public Guid? AdminAprovedWinnerId { get; set; }
public Guid TeamAId { get; set; }
public Guid TeamBId { get; set; }
public int TeamAVote { get; set; }
public int TeamBVote { get; set; }
public ClientMatch()
{
}
public ClientMatch(MatchWithTmpId noGuid)
{
...
}
}
As you will notice, the Round object is a Code First model with Virtual attributes. I have removed it from RoundModel just prior to uploading this question to test it, and removing it doesn't resolve the issue.
and my Ajax post
Edit: thanks to Nick Bailey, I started to find heaps of issues edits to the following:
POST http://localhost:52690/Admin/StartRound HTTP/1.1
Host: localhost:52690
Connection: keep-alive
Content-Length: 752
Accept: */*
Origin: http://localhost:52690
X-Requested-With: XMLHttpRequest
__RequestVerificationToken: TU5lBruq0K0FBxviWOS1GVjtRFw0edbCvE57bzh3wikqlXTw384jgxGBic61nMgUNwAXRgbf50cpk0naKADQgwnR9aNq1R55SSHj6UvszBRdfJ8nt362OFBQLC7eWLTwAwPJUVkRrFQkCOnZwtL6SQ2
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:52690/Admin/MatchScheduler
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: redacted
{
"RoundModel":{
"Matches":[
{
"SystemId":null,
"OldId":0,
"RoundName":"awd",
"ServerName":"Apogawd0",
"ServerPassword":"apog",
"ServerMessage":"Can Team \"Lovin it\" please create server \"Apogawd0\" hosted in Oceania Servers, random map",
"AdminAprovedWinnerId":null,
"TeamAId":"74206e93-33aa-48d4-bac2-5f9acac0be90",
"TeamBId":"35d4be62-4e3e-4575-8ce9-6c819382b50c",
"TeamAVote":1,
"TeamBVote":1
}
]
}
}
Any and all help appreciated, Cheers, Michael.
edit cont: I have made allot of changes thanks to Nick remdining me of the basics, haha I have spent too much time in JS land. Still getting null on Matches
Upvotes: 0
Views: 200
Reputation: 1535
The final piece of the puzzle was a frustrating one. I stopped using JSON as form data and went back to my raw js object and all a sudden, success! So I looked again at the headers and sure enough: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
so I went into my Ajax method and added: contentType: "application/json"
Finally :) Success, thanks Nick Bailey, it was you who got me on the right track so I will be awarding you the answer.
Please edit your Question to indicate that the full answer is in my answer, or just update yours to include the solution.
Thanks again!
Upvotes: 0
Reputation: 3162
You're passing an empty object for your round parameter, so naturally it comes in null. TeamAVote and TeamBVote are non nullable fields on your client match model, so the Jason serialized can't parse the null values you posted. I'd make those fields nullable.
Also, it's usually a really good idea to use different models for your API models and data models. The usually differ enough that shared code becomes a problem.
Upvotes: 1