【发布时间】:2020-06-03 22:30:27
【问题描述】:
我的 index.cshtml 剃须刀页面上有以下 ajax 帖子,效果很好:
create: {
url: "/api/LearningTasks/create",
type: "POST",
dataType: "json"
},
它将帖子发送到我的控制器,并且代码接收工作正常。它看起来像这样:
[HttpPost]
[Route("api/LearningTasks/create")]
public async Task<ActionResult<LearningTask>> CreateLearningTask(LearningTask learningTask)
{
_context.LearningTasks.Add(learningTask);
await _context.SaveChangesAsync();
return CreatedAtAction("GetLearningTask", new { id = learningTask.Id }, learningTask);
}
我想更改帖子的目标,使其转到“代码隐藏”index.cshtml.cs。我想收到它的方法是这样的:
public async Task<IActionResult> OnPostAsync()
{
// This is where I want to have the send the data for the create operation instead of to /api/LearningTasks/create
_context.LearningTasks.Add(LearningTask);
await _context.SaveChangesAsync();
return null;
}
我已尝试删除行 url: "/api/LearningTasks/create", 并将其设置为 url: "", 但均无效。任何帮助弄清楚如何做到这一点将不胜感激。
【问题讨论】:
标签: jquery asp.net-core razor-pages