Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Routing in MVC ASP.NET Core with Controllers and Views

I’m still trying to explore the Routing but I couldn’t get the job done. I get confused on how do I use the Views and Controllers, I am trying to achieve like http://localhost:5036/Dashboard/Products because I’m creating a CRUD app but trying in different Routes but it is popping up an error and the only thing I can think of is that about the file path or the Controllers.

I have an sample image for which the Products is inside the Dashboard.

Dashboard

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First I have the _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>
...

And the 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);
        }
    }
}

And the 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 });
        }


    }
}

And this are the error I keep receiving. After clicking in the _DashboardLayout.cshtml links

Error 1

Error 2

>Solution :

you can set exact location of your view with

return View("~/Views/yourPath.cshtml")

or you can read this
link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading