[英]Getting Null from Ajax Post
I am trying to get my model from inside my web app, into a post Action. 我试图将我的模型从我的Web应用程序中导入到后期操作中。 the only issue is that I get a model object which has 1 null variables inside :(. The action is:
唯一的问题是,我得到了一个在:(内具有1个空变量的模型对象。动作是:
[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: 编辑:感谢Nick Bailey,我开始发现许多对以下内容进行编辑的问题:
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. 您将注意到,Round对象是具有虚拟属性的Code First模型。 I have removed it from RoundModel just prior to uploading this question to test it, and removing it doesn't resolve the issue. 我已在上载此问题以对其进行测试之前将其从RoundModel中删除,并且将其删除并不能解决问题。
and my Ajax post 和我的Ajax帖子
Edit: thanks to Nick Bailey, I started to find heaps of issues edits to the following: 编辑:感谢Nick Bailey,我开始发现许多对以下内容进行编辑的问题:
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. 编辑续:感谢尼克让我了解了基础知识,我已经进行了更改,哈哈,我在JS领域花费了太多时间。 Still getting null on Matches
比赛仍然无效
You're passing an empty object for your round parameter, so naturally it comes in null. 您要为round参数传递一个空对象,因此自然而然地将其为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.
TeamAVote和TeamBVote在客户端匹配模型上是不可为空的字段,因此序列化的Jason无法解析您发布的空值。 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. 另外,对您的API模型和数据模型使用不同的模型通常是一个好主意。 The usually differ enough that shared code becomes a problem.
通常,它们之间的差异足以使共享代码成为问题。
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!
我停止使用JSON作为表单数据,然后回到原始js对象,突然间成功! So I looked again at the headers and sure enough:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
因此,我再次查看了标头并确定:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
so I went into my Ajax method and added: contentType: "application/json"
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
所以我进入了我的Ajax方法并添加: 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. 最后:)成功,谢谢尼克·贝利(Nick Bailey),正是您使我步入正轨,所以我将为您提供答案。
Please edit your Question to indicate that the full answer is in my answer, or just update yours to include the solution. 请编辑您的问题以表明完整答案已包含在我的答案中,或者仅更新您的问题以包含解决方案。
Thanks again! 再次感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.