ASP.NET Web API POST parameter is null

前端 未结 2 1981
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 08:43

(To clarify, I have read other questions and their answers and tried all solutions, but none of them solved the issue)

I\'m debugging locally a code that I\'ll deplo

相关标签:
2条回答
  • 2021-01-13 09:13

    For what it's worth, I got sick of trying to get it to serialize correctly and did it myself.

    My data is a javascript array of strings which posts like "value=thing1,thing2,thing3" and I was able to serialize it using the following:

        $('<form>', {
            "id": "dynamicForm",
            "html": '<input type="hidden" id="val" name="val" value="' + $scope.items + '" />',
            "action": '/api/Example/',
            "method": "post"
        }).appendTo(document.body).submit();
    

    Then on the server side:

    [HttpPost]
    [Route("")]
    public HttpResponseMessage Post()
    {
        var values = HttpContext.Current.Server.UrlDecode(Request.Content.ReadAsStringAsync().Result).Replace("val=","").Split(',');
    ...
    }
    
    0 讨论(0)
  • 2021-01-13 09:17

    I believe the default model binder does not bind to fields, only properties with a public setter. Try changing your fields to properties:

    public class PlayerEntry
    {
        public string userID { get; set; }
        public string userName { get; set; }
        public int userProgress { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题