Home > Enterprise >  Find URL submit path of a Razor inherited page controller method
Find URL submit path of a Razor inherited page controller method

Time:11-19

Usually, I send data to a controller with a URL using the class name followed by the method name /Parent?handler=InitData or /Parent/InitData if the @page "{handler?}" is defined in a page.

However, how can this be done with a child controller than inherits the main parent controller ?

What I am looking for is to call a method inside a child controller with a syntax that is similar to this /Parent/Child/Test. Note that the call is made within a partial page embedded inside the main parent page.

Any idea on how this can be achieved ? (I know it has something to do with routing but I just don't understand how to do it)


Here is what I have so far

Partial page

@using App.Models.PagesViews.PartialPage
@model PartialPage // <-- partial parent model has access to PartialPage_Add and PartialPage_Delete children

<script type="module">
    $.ajax({
        url: "/Parent/Child/Test", <-- should call child controller method but is not working!!!

        type: "post",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        ...
    });
</script>

<div>
    <!--... html code...-->
<div>

Parent page

@page "{handler?}"
@model App.Pages.ParentModel

@section head {
    ...
}

<div>
    <partial name="_PartialPage" model="Model.partialPage_Add"

    ...

    <partial name="_PartialPage" model="Model.partialPage_Delete"
</div>

Parent page controller

public class ParentModel : PageModel
{
    private readonly DatabaseContext _dbContext;

    public readonly PartialPage_Add partialPage_Add; // <-- PartialPage_Add is a child of PartialPage
    public readonly PartialPage_Delete partialPage_Delete; // <-- PartialPage_Delete is a child of PartialPage

    public MainModel(DatabaseContext _dbContext) 
    {
        this._dbContext = _dbContext;
    }

    public IActionResult OnGet() => Page();
}

Child page controller

public class ChildModel : ParentModel
{
    public ChildModel(DatabaseContext _dbContext) : base(_dbContext)
    {
    
    }

    public void OnPostTest() // <-- Access this method from partial page
    {
        Log.Debug($"Successfully called method Test() in class ChildModel");

        ....
    }
}

CodePudding user response:

I think you may misunderstand the usage of Razor Pages and MVC Controller.

Razor Pages should contains .cshtml file declared with @Page and .cshtml.cs file with same name of the .cshtml file. Also the .cshtml.cs file should inherits PageModel or the inheritance class of the PageModel.

Your ChildModel extends the ParentModel which is the inheritance class of PageModel, if your project does not contain corresponding .cshtml, the ChildModel is just a simple class and cannot be called by ajax.

So you need be sure your project contains a Child.cshtml and declare @page "{handler?}" in it. And then the correct url should be /Child/Test.

If you want to use the parent as the suffix, change the page route template to @Page "/Parent/Child/{handler?}".

Besides, you must send the AntiForgeryToken together with the data you want to the Post method if you use ajax in Razor Pages.

Reference:

How to take result value of a jQuery script into a C# code in the same razor page in ASP.NET Core MVC?

For MVC controller, no matter your project contains the .cshtml file or not, you can also hit the action, because you can configure the route template in the Startup.cs/Program.cs or just use [Route] attribute.

  • Related