Getting Null from Ajax Post

时间:2015-09-29 00:41:38

标签: c# ajax asp.net-mvc asp.net-mvc-5

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:

  • removed Round as it wasn't needed
  • changed Matches to List as not sure if model constructor defaults Interfaces
  • changed OldId to int as that is the only type it will ever be and Json object had it as an int coming up to the action
  • changed SystemId and AdminAprovedWinnerId to nullable as both are expected to be able to be null
  • un-redacted all of constructors, I found out that I had no default constructor so therefore there was no way for it to be constructed
  • added default constructor as mentioned above
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:

  • SystemId now passes null as 0 cannot parse into GUID
  • TeamAVote, TeamBVote both passing through 1 (which coresponds to an enum)
  • Currently I have them nested inside of RoundModel as it was what I was last trying to get it working
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

2 个答案:

答案 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,是你让我走上正轨,所以我会给你答案。

请编辑您的问题以表明我的答案中有完整的答案,或者只是更新您的答案以包含解决方案。

再次感谢!