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

发布于 2025-01-11 04:09:50 字数 1288 浏览 0 评论 0原文

我在 .net core 中有一个发布请求。当我使用 AJAX 调用它时,该方法被命中,但我的所有属性值均为 null。网上有很多解决这个问题的例子,但我似乎找不到一个有效的。事实上,我在这个项目中有很多 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

这是我的模型类:

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

I have a post request in .net core. When I call it using AJAX, the method is hit, but all my attribute values are null. There are many examples of fixing this around the web, but I can't seem to find one that works. In fact, I have many POST requests in this project, and they all work fine, yet this one does not which leads me to believe I have something simple that I'm just not seeing.

Here is the simplified AJAX call:

    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);
    }
});

Here is my controller code:

    [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

Here is my model 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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2025-01-18 04:09:50

以下是将数据发送到 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;}
}

Here is an alternative way to send your data to your Controller method:

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;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文