简体   繁体   中英

Getting Null from Ajax Post

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

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.

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 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM