Error: "Web Page is not working"

Kenneth R. Jones 46 Reputation points
2024-05-06T15:53:48.09+00:00

I'm using ASP.Net Core MVC with Identity Framework. I added a new category model:


    public class Category
    {

#pragma warning disable CS8618

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        [MaxLength(30)]
        public string Name { get; set; }

        [Required]
        public string Slug { get; set; }

#pragma warning restore CS8618

    }

I generated the code for the controller. This should be the relevant parts:

        // GET: Categories/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: 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,Slug")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(category);
        }

And then I have the view:

@model KRJWeb.Models.Category

<h4>Category</h4>

<form asp-action="Create" method="post">
    <div>
        <label asp-for="Name"></label>
        <input asp-for="Name" />
    </div>
    <div>
        <label asp-for="Slug"></label>
        <input asp-for="Slug" />
    </div>
    <div>
        <input type="submit" value="Create" />
    </div>
</form>

The Details and Index pages work fine. They load data from the database. The Create and Delete don't work. At first, when I clicked the "create" button it was loading the GET method instead of the POST. I changed the form above and added method="post".

Now, every time I click the create category button, I get an error:

400: This page isn't working right now.

I have no idea why that error is showing, and I can't find the details. I have checked the VS console. I've tried enabling a logger. I've checked the Event Viewer in Windows, and I can't find anything to tell me why that page is showing up.

I added breakpoints in the controller, but none of the code there is ever hit. It's like the page instantly dies without executing any code.

I found this in the Edge developer console:

error

I don't really have any idea what that's trying to tell me. I don't recognize the "classList" it's talking about, so I have no idea what that means.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kenneth R. Jones 46 Reputation points
    2024-05-06T16:58:48.8133333+00:00

    I got it working, after several hours of messing with it. The tag helpers weren't working and they were showing up in the HTML source instead of the HTML they were supposed to generate. I found the answer in ChatGPT after pasting all my code in there.

    I'll leave this here in case anyone else has the same issue in the future. Here were the steps to fix it:


    Yes, you should create a _ViewImports.cshtml file if it doesn't already exist in your project. This file is used to import namespaces and configure tag helpers for Razor views in ASP.NET Core MVC projects.

    Here's how you can create a _ViewImports.cshtml file and add the necessary tag helper directive to enable tag helpers:

    1. Create the File:
      • Right-click on the folder where you want to create the _ViewImports.cshtml file (typically in the Views folder).
      • Choose "Add" > "New Item...".
      • Select "Razor View Imports" from the list of templates.
      • Name the file _ViewImports.cshtml and click "Add".
    2. Add Tag Helper Directive:
      • Open the _ViewImports.cshtml file.
      • Add the following tag helper directive:
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    

    This directive imports all tag helpers from the Microsoft.AspNetCore.Mvc.TagHelpers namespace, allowing you to use them in your Razor views.

    1. Save the File:
      • Save the _ViewImports.cshtml file.

    Once you've created and saved the _ViewImports.cshtml file with the tag helper directive, it will be applied to all Razor views within the same directory and its subdirectories, allowing you to use tag helpers like asp-for without explicitly importing namespaces or configuring tag helpers in each individual view file.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.