Simon Bailey
Simon Bailey

Reputation: 103

Why do my Razor Page methods not get called?

I cannot get Razor Pages to work. It will display the Index page and thats it. No links work on the page and no data will display. All Helper Methods are completely ignored by the compiler, it will not even highlight them in a different colour.

When the page loads it will call the Get function and the page will display, but none of the functionality on the view works. I have deleted everything so I just have 1 page with a couple of post buttons on it, and still it will not call the Post method. Does anyone have any idea what is going on?

Here is my View:

@page
@model FloorCore.Areas.Jobs.Pages.JobIndexModel
@{
}

<form method="post">
    <button class="btn btn-default">Click to post</button>
    <button type="submit" class="btn btn-primary">Click to post</button>
</form>

Here is the controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace FloorCore.Areas.Jobs.Pages
{
    public class JobIndexModel : PageModel
    {
        public void OnGet()
        {
            int for_breakpoint = 0;
        }

        public void OnPost()
        {
            int for_breakpoint  = 0;
        }
    }
}

Clicking on either button just returns: This page isn’t working. If the problem continues, contact the site owner. HTTP ERROR 400

The OnPost method on the controller is not called.

Upvotes: -1

Views: 3935

Answers (1)

Simon Bailey
Simon Bailey

Reputation: 103

I came back to answer my own question because I figured out what the problem was and perhaps this will help someone else. I was missing the _ViewImports file in this particular Area.

The _ViewImports contained the following line:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

which I needed in order for my helper methods to work.

Upvotes: 2

Related Questions