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
答案 0 :(得分:1)
你正在为你的圆形参数传递一个空对象,所以它自然就是null。 TeamAVote和TeamBVote在您的客户端匹配模型上是不可为空的字段,因此Jason序列化无法解析您发布的空值。我会让这些字段可以为空。
此外,为API模型和数据模型使用不同的模型通常是一个非常好的主意。通常不同的是共享代码成为问题。
答案 1 :(得分:0)
最后一块拼图令人沮丧。我停止使用JSON作为表单数据并返回到我的原始js对象,突然,成功!所以我再次查看标题并确定:Content-Type: application/x-www-form-urlencoded; charset=UTF-8
所以我进入了我的Ajax方法并添加了:contentType: "application/json"
最后:)成功,感谢Nick Bailey,是你让我走上正轨,所以我会给你答案。
请编辑您的问题以表明我的答案中有完整的答案,或者只是更新您的答案以包含解决方案。
再次感谢!