Patel
Patel

Reputation: 48

OnPost in Area with Razor page not working

(Using ASP.NET Core 3.1 - Razor Pages web application)

I am trying to trigger OnPost method which is in Areas >> Login >> Pages >> Index.cshtml.cs Razor Page. Somehow, it triggers OnGet method but when I try to post a form, it didn't trigger OnPost method.

Index.cshtml.cs

namespace WebApplication1.Areas.Login.Pages
{    
public class IndexModel : PageModel
        {
            public void OnGet()
            {
    //triggering
            }
            public void OnPost()
            {
    //not triggering
            }
        }
}

Index.cshtml

@page
@model WebApplication1.Areas.Login.Pages.IndexModel
@{
}

<form method="post">

    <input name="UserId" class="form-control" />        

    <input type="submit" value="Create" class="btn btn-primary" />
</form>

Note: If I am not using Areas, OnGet and OnPost working fine.

I have tried below but none of them worked for me:

  1. Tried https://www.learnrazorpages.com/advanced/areas
  2. How can Add Area in razor pages on dot net core 3.0/3.1?

Upvotes: 1

Views: 2616

Answers (1)

Patel
Patel

Reputation: 48

I found the answer here: I have added the following code in Areas → Login → Pages

_ViewImports.cshtml:

@using WebApplication1
@namespace WebApplication1.Web.Areas.Login.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Upvotes: 1

Related Questions