英文:
ASP.NET MVC Project: Why is my form not accessing my controller method?
问题
当我尝试提交我的'Create.cshtml'文件中的表单时,我被引导到一个"HTTP ERROR 405"页面。提交根本没有访问控制器中的函数。
这是控制器的POST函数:
// POST: Books/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CreateViewModel model)
{
Console.WriteLine("Hello from create post function");
// 空值检查
if (string.IsNullOrEmpty(model.title) || string.IsNullOrEmpty(model.author) || model.yearPublished == null)
return NotFound();
// 创建图书
var book = new Book {
Id = Guid.NewGuid(),
Title = model.title,
Author = model.author,
YearPublished = model.yearPublished
};
// 检查图书ID是否唯一
while (true) {
// 查看图书是否存在
var temp_book = await _context.Books.FirstOrDefaultAsync(b => b.Id == book.Id);
// 如果不存在,中断循环,否则重新分配GUID
if (temp_book == null)
break;
else
book.Id = Guid.NewGuid();
}
// 将图书添加到Books表格并保存
_context.Books.Add(book);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
这是带有无法连接的表单的'Create.cshtml'文件:
@model Deliverable4.Controllers.CreateViewModel
@{
ViewData["Title"] = "Create";
}
<h1>@ViewData["Title"]</h1>
<div>
<p>CREATE PAGE</p>
</div>
<form asp-action="Create" method="post">
<label for="title">Title</label>
<input name="title" id="title">
<label for="author">Author</label>
<input name="author" id="author">
<label for="yearPublished">Year</label>
<input name="yearPublished" id="yearPublished">
<input type="submit" value="Submit">
</form>
有人能告诉我问题是什么吗?
英文:
When I try and submit the form in my 'Create.cshtml' file, I am brough to a "HTTP ERROR 405" page. The submission is not at all accessing the function in the controller.
Here is the controller post function:
// POST: Books/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CreateViewModel model)
{
Console.WriteLine("Hello from create post function");
// null check
if (string.IsNullOrEmpty(model.title) || string.IsNullOrEmpty(model.author) || model.yearPublished == null)
return NotFound();
// Create Book
var book = new Book {
Id = Guid.NewGuid(),
Title = model.title,
Author = model.author,
YearPublished = model.yearPublished
};
// Check if book ID is original
while (true) {
// see if book exists
var temp_book = await _context.Books.FirstOrDefaultAsync(b => b.Id == book.Id);
// if it does break, else reassign the GUID
if (temp_book == null)
break;
else
book.Id = Guid.NewGuid();
}
// Save book to Books Table + Save
_context.Books.Add(book);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
And here is the 'Create.cshtml' file with the form that is not connecting:
@model Deliverable4.Controllers.CreateViewModel
@{
ViewData["Title"] = "Create";
}
<h1>@ViewData["Title"]</h1>
<div>
<p>CREATE PAGE</p>
</div>
<form asp-action="Create" method="post">
<label for="title">Title</label>
<input name="title" id="title">
<label for="author">Author</label>
<input name="author" id="author">
<label for="yearPublished">Year</label>
<input name="yearPublished" id="yearPublished">
<input type="submit" value="Submit">
</form>
Can anyone tell me the issue?
答案1
得分: 1
问题似乎是你告诉控制器验证AntiForgeryToken,但在表单中没有提供AntiForgeryToken。在你的表单中添加AntiForgeryToken,错误应该会解决。
<form method="post" action="Create" >
@Html.AntiForgeryToken()
<!--你的表单其余部分-->
</form>
英文:
The problem seems to be that you tell your controller to validate the AntiForgeryToken but you do not provide the AntiForgeryToken in your Form. Add the AntiForgeryToken in your Form and the error should be resolved.
<form method="post" action="Create" >
@Html.AntiForgeryToken()
<!--rest of your Form elements-->
</form>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论