如何在ASP.NET Razor页面中使用锚定标签作为提交按钮

发布于 2025-01-28 12:03:29 字数 718 浏览 1 评论 0 原文

我在Razor页面中有一个处理程序方法,名称 ongetExpense(usePayload有效载荷)

page.cshtml:

<input type="hidden" asp-for="Filters.From" value="@Model.Filters.From.ToYYYYMMDD()" />
<input type="hidden" asp-for="Filters.To" value="@Model.Filters.To.ToYYYYMMDD()" />
<a asp-page-handler="Expense" class="btn btn-primary" type="button" style="border-color: inherit">
Expense
</a>

page.cshtml.cs:

public IActionResult OnGetExpense(UserPayload filter)
{
    Console.WriteLine(filter.From);
    Console.WriteLine(filter.To);
    return Page();
}

来自日期,我所得到的是<代码> 01-01-0001 它没有提交我需要的有效载荷。

有没有标签属性,我可以带有锚标签的有效载荷?

请帮忙

I have a handler method in razor page with name OnGetExpense(UsePayload payload)

page.cshtml:

<input type="hidden" asp-for="Filters.From" value="@Model.Filters.From.ToYYYYMMDD()" />
<input type="hidden" asp-for="Filters.To" value="@Model.Filters.To.ToYYYYMMDD()" />
<a asp-page-handler="Expense" class="btn btn-primary" type="button" style="border-color: inherit">
Expense
</a>

page.cshtml.cs:

public IActionResult OnGetExpense(UserPayload filter)
{
    Console.WriteLine(filter.From);
    Console.WriteLine(filter.To);
    return Page();
}

The From date and to date I am getting is 01-01-0001 it is not submitting the payload I needed.

Is there any tag attribute I can sen the payload with anchor tag?

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2025-02-04 12:03:29

您的渲染标签将创建到端点的GET请求,您需要到帖子以发送请求正文中的数据,并使控制器将请求主体绑定到您的用户Payload对象。

要发送邮政请求,您可以写一些JavaScript并将其发布到JSON到终点,也可以以形式包装代码。

您的控制器需要具有 httppost 分配的属性。

[HttpPost]
public IActionResult OnGetExpense(UserPayload filter)
{
    Console.WriteLine(filter.From);
    Console.WriteLine(filter.To);
    return Page();
}

Your rendered A tag will create a GET request to the endpoint, you need to to a POST to send the data in the body of the request and for the Controller to bind the Request Body to your UserPayload object.

To send the POST request you can either write some JavaScript and POST to JSON to the end point or wrap the code in a Form.

https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/4-working-with-forms

Your controller will need to have HttpPost attribute assigned.

[HttpPost]
public IActionResult OnGetExpense(UserPayload filter)
{
    Console.WriteLine(filter.From);
    Console.WriteLine(filter.To);
    return Page();
}
清晨说晚安 2025-02-04 12:03:29

有没有标签属性,我可以用锚标签换取有效载荷?

您可以在下面使用 ASP-Route- {value}

是一个工作演示,您可以参考它。

cshtml:

 <a  asp-page-handler="Expense" asp-route-attendeeid="12" class="btn btn-primary" type="button" style="border-color: inherit">Expense</a>

cshtml.cs

 public IActionResult OnGetExpense(int attendeeId)
        {
            return Page();
        }

结果:

”在此处输入图像描述”

Is there any tag attribute I can sen the payload with anchor tag?

You can use asp-route-{value}

Below is a work demo, you can refer to it.

cshtml:

 <a  asp-page-handler="Expense" asp-route-attendeeid="12" class="btn btn-primary" type="button" style="border-color: inherit">Expense</a>

cshtml.cs

 public IActionResult OnGetExpense(int attendeeId)
        {
            return Page();
        }

result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文