我仍在尝试探索路由,但我无法完成工作。我对如何使用视图和控制器感到困惑,我试图实现像 http://localhost:5036/Dashboard/Products 这样的目标,因为我正在创建一个 CRUD 应用程序,但尝试在不同的路由中,但它弹出一个错误我唯一能想到的是关于文件路径或控制器。
我有一个示例图像,其产品位于仪表板内。
首先我有
_DashboardLayout.cshtml
...
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Dashboard" asp-action="Index">
Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Products"
asp-action="Index">Products</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Products"
asp-action="Create">Create Product</a>
</li>
</ul>
...
还有
Controller/ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Crud.Entities;
using Crud.Models;
using System.Threading.Tasks;
namespace Crud.Controllers.Dashboard
{
// [Route("Dashboard/[controller]")]
public class ProductsController : Controller
{
private readonly AppDbContext _context;
public ProductsController(AppDbContext context)
{
_context = context;
}
// GET: Dashboard/Products
[HttpGet]
public async Task<IActionResult> Index()
{
var products = await _context.Products.ToListAsync();
return View("Dashboard/Products/Index", products);
}
// GET: Dashboard/Products/Create
[HttpGet("Create")]
public IActionResult Create()
{
return View("Dashboard/Products/Create");
}
// POST: Dashboard/Products/Create
[HttpPost("Create")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,ProductName,ProductBrand, Quantity, TypeOfProduct, Price, AgeRange")] Product product)
{
if (ModelState.IsValid)
{
_context.Add(product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View("Dashboard/Products/Create", product);
}
// GET: Dashboard/Products/Edit/5
[HttpGet("Edit/{id:int}")]
public async Task<IActionResult> Edit(int? id)
{
if (id == null) return NotFound();
var product = await _context.Products.FindAsync(id);
if (product == null) return NotFound();
return View("Dashboard/Products/Edit", product);
}
// POST: Dashboard/Products/Edit/5
[HttpPost("Edit/{id:int}")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,ProductName,ProductBrand, Quantity, TypeOfProduct, Price, AgeRange")] Product product)
{
if (id != product.Id) return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(product);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(product.Id)) return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View("Dashboard/Products/Edit", product);
}
// GET: Dashboard/Products/Delete/5
[HttpGet("Delete/{id:int}")]
public async Task<IActionResult> Delete(int? id)
{
if (id == null) return NotFound();
var product = await _context.Products.FindAsync(id);
if (product == null) return NotFound();
return View("Dashboard/Products/Delete", product);
}
// POST: Dashboard/Products/Delete/5
[HttpPost("Delete/{id:int}")]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var product = await _context.Products.FindAsync(id);
_context.Products.Remove(product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool ProductExists(int id)
{
return _context.Products.Any(e => e.Id == id);
}
}
}
还有
DashboardController.cs
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using Crud.Models;
namespace Crud.Controllers
{
public class DashboardController : Controller
{
public IActionResult Index()
{
// Add any additional logic for the dashboard here
return View(); // Return the dashboard view
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
这是我不断收到的错误。单击
_DashboardLayout.cshtml
链接后