Search code examples
c#asp.net-coreasp.net-core-2.2

Razor Page Handler with Class as Arg


How can I create a handler that takes in a class as an arg. This is based of the mediatr example for Razor pages. In the example, the 'Query' object has only an 'Id' property. I want to have 2 properties, for example:

public async Task OnGetEditAsync(Query query)
{
    Data = await _mediator.Send(query);
}

public class Query
{
    public string FirstId { get; set; }
    public string SecondId { get; set; }
}

When I try to create a route like this in my cshtml:

<a asp-page="/TestPage" asp-route-FirstId="abc" asp-route-SecondId="123">Test Link</a>

I get a 400 error. Is it possible to do this?


Solution

  • Link is pointing to wrong handler, You have OnGetEditAsync where Edit is name of handler; try this:

    <a asp-page="TestPage" asp-page-handler="Edit" asp-route-FirstId="abc" asp-route-SecondId="123">Test Link</a>