Ajax 使用模型将 Null 值传递给控制器
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是将数据发送到
Controller
方法的另一种方法:AJAX
:Controller
:Model
:Here is an alternative way to send your data to your
Controller
method:AJAX
:Controller
:Model
: