Home » c# » c# – Action button without Form | ASP.NET CORE Razor Pages-Exceptionshub

c# – Action button without Form | ASP.NET CORE Razor Pages-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I want a tag that basically performs a post action on my razor page model including a parameter. Ideally I don’t want to have a form around it and have the linking to the according action in the href="post-to-action"

in MVC I was always able to call a controller action without having a form around a button/ with the following:

 <a href="@Url.Action("Information", "Admin", new { id = UrlParameter.Optional })">Add an Admin</a>

This doesn’t work for razor pages im pretty sure. Im struggling to find an alternative in razor pages. Of course ajax is an option but I didn’t find an alternative to make a post call in razor pages without having a around it.

Does anybody have a workaround without having to submit a form with javascript or something along those lines?

How to&Answers:

In asp.net core razor pages , you can redirect user to another page with parameter like :

<a asp-page="/pageName" asp-route-id="@Model.ID">redirect</a>

But that is a Get request , you can’t used to post data . You stil need to post use form :

https://www.learnrazorpages.com/razor-pages/handler-methods

Or use js to collect data and send to another page using AJAX .