Ajax 使用模型将 Null 值传递给控制器

Ajax pass Null values to controller using model

我在 .net 核心中有一个 post 请求。当我使用 AJAX 调用它时,该方法被命中,但我所有的属性值都是空的。网上有很多解决这个问题的例子,但我似乎找不到一个可行的。事实上,我在这个项目中有很多 POST 请求,它们都工作正常,但这个没有,这让我相信我有一些我只是没有看到的简单东西。

这是简化的 AJAX 调用:

    let questionChoiceData = {
    'QuestionID':'1',
    'ChoiceID':'0',
    'SurveyID':'1',     
    'ResponseText':'Did I work?',
    'DeleteFlag': '0'
};

$.ajax({
    type: 'POST',
    url: '/Create_Edit',
    contentType: "application/json",
    dataType: 'json',
    data: JSON.stringify(questionChoiceData),
    success: function (data) {
        console.log(data);
    },
    error: function (request, status, error) {
        console.log(error);
    }
});

这是我的控制器代码:

    [IgnoreAntiforgeryToken]
    [Route("[action]")]
    [HttpPost]
    public IActionResult Create_Edit([FromBody] Choice_Model v)
    {
        //Everything below was deleted for brevity sake. Just know that When I set a breakpoint, V shows the model with it's attributes, but no attribute values.

       
    }//Method

这是我的模型class:

    public class Choice_Model
{
    string QuestionID { get; set; }
    string ChoiceID { get; set; }
    string SurveyID { get; set; }
    string ResponseText { get; set; }
    string DeleteFlag { get; set; }
}

这是将数据发送到 Controller 方法的另一种方法:

AJAX:

let questionChoiceData = {
'QuestionID':'1',
'ChoiceID':'0',
'SurveyID':'1',     
'ResponseText':'Did I work?',
'DeleteFlag': '0'
};

var json = {
   questionChoiceData: questionChoiceData
};


$.ajax({
    type: 'POST',
    url: '/Create_Edit',
    dataType: 'json',
    data: {'json': JSON.stringify(json)},
    success: function (data) {
    console.log(data);
    },
    error: function (request, status, error) {
    console.log(error);
    }
});

Controller:

using Newtonsoft.Json;

[IgnoreAntiforgeryToken]
[Route("[action]")]
[HttpPost]
public IActionResult Create_Edit(string json)
{
   Root rootData = new Root();
   Choice_Model choice_model = new Choice_Model();
   if(!string.IsNotNullOrEmpty(json))
   {
     //Get the data here in your model
     rootData=JsonConvert.DeserializeObject<Root>(json);
     choice_model = rootData.questionChoiceData;
   }

}//Method

Model:

public class Choice_Model
{
    string QuestionID { get; set; }
    string ChoiceID { get; set; }
    string SurveyID { get; set; }
    string ResponseText { get; set; }
    string DeleteFlag { get; set; }
}

public class Root
{
  public string json {get;set;}
  public Choice_Model questionChoiceData {get;set;}
}