У меня есть образец изображения, для которого продукты находятся внутри панели мониторинга.
Панель мониторинга
Сначала у меня есть _DashboardLayout.cshtml
Код: Выделить всё
...
[list]
[*]
Dashboard
[*]
Products
[*]
Create Product
[/list]
...
Код: Выделить всё
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 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 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 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 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 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 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);
}
}
}
Код: Выделить всё
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 });
}
}
}
Ошибка 1Ошибка 2
Подробнее здесь: https://stackoverflow.com/questions/790 ... -and-views
Мобильная версия