[英]Action from page not handled in controller in ASP.NET CORE
I created controller and views with default utility in VS that generate code.我创建了 controller 并在 VS 中使用默认实用程序生成代码的视图。
When I click on create button on Create page I always have exception and breakpoints in Create method not working.当我单击创建页面上的创建按钮时,我总是在 Create 方法中出现异常和断点不起作用。 I try change some handlers, change arguments or change
asp-action
for form but I couldn't enter to Create handler.我尝试更改一些处理程序,更改 arguments 或更改表单
asp-action
,但我无法进入创建处理程序。
How I can fix it?我该如何解决?
Create page创建页面
@model BulkyBook.Models.Category
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<h4>Category</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Part of controller core that not working部分 controller 内核不工作
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using BulkyBook.DataAccess.Data;
using BulkyBook.Models;
namespace BulkyBook.Areas.Admin.Controllers
{
[Area("Admin")]
public class CategoriesController : Controller
{
private readonly ApplicationDbContext _context;
public CategoriesController(ApplicationDbContext context)
{
_context = context;
}
// GET: Admin/Categories
public async Task<IActionResult> Index()
{
return View(await _context.Categories.ToListAsync());
}
// GET: Admin/Categories/Create
public IActionResult Create()
{
return View();
}
// POST: Admin/Categories/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name")] Category category)
{
if (ModelState.IsValid)
{
_context.Add(category);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(category);
}
}
}
Exception例外
Can you describe what kind of exception that you are getting when you clicking the C reate Button您能否描述单击 C按钮时遇到的异常类型
When I click on create button on Create page I always have exception and breakpoints in Create method not working.
当我单击创建页面上的创建按钮时,我总是在 Create 方法中出现异常和断点不起作用。 I try change some handlers, change arguments or change asp-action for form but I couldn't enter to Create handler.
我尝试更改一些处理程序,更改 arguments 或更改表单的 asp-action,但我无法进入创建处理程序。
It seems that you create and use Areas feature in your project, please check if you create/copy _ViewImports.cshtml
file with following code snippet to the appropriate view folder under areas, which could help make Tag Helpers available for your areas view(s).您似乎在项目中创建和使用区域功能,请检查您是否使用以下代码片段创建/复制
_ViewImports.cshtml
文件到区域下的相应视图文件夹,这有助于使标签助手可用于您的区域视图.
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
For more information, please check this doc:有关更多信息,请查看此文档:
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0#_viewimportscshtml https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0#_viewimportscshtml
Besides, if your view page Create
is not in the same area, please explicitly asp-area="admin"
for your form tag helper.此外,如果您的视图页面
Create
不在同一区域,请为您的表单标签助手显式asp-area="admin"
。
<form asp-action="Create" asp-area="Admin" method="post">
Please try above bold area.请尝试以上粗体区域。
And let me know if it's not work.如果它不起作用,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.