简体   繁体   English

单击后如何使用页面处理程序禁用 Razor 提交按钮并将其 POST 到正确的处理程序

[英]How to disable a Razor submit button with a page handler after click and have it POST to the correct handler

I have a form on a Razor page with 2 submit buttons each using a different page handler.我在 Razor 页面上有一个表单,其中有 2 个提交按钮,每个按钮使用不同的页面处理程序。 I'd like to disable the button and submit the form on click having the form post to the correct page handler for the button clicked.我想禁用按钮并在单击时提交表单,将表单发布到单击按钮的正确页面处理程序。 I know this is achievable using jQuery without the form handler, just not sure how to get the form to post to the correct page handler from JavaScript or if there is another approach.我知道在没有表单处理程序的情况下使用 jQuery 是可以实现的,只是不确定如何让表单从 JavaScript 发布到正确的页面处理程序,或者是否有其他方法。

.CSHTML .CSHTML

<form id="editBudgetForm" method="post">
    <button type="submit" id="btnSaveRevenue" class="btn btn-primary" asp-page-handler="BudgetRevenue" title="Save the budget revenue.">
        Save Revenue
    </button>
    <button type="submit" id="btnSaveExpense" class="btn btn-primary" asp-page-handler="BudgetExpense" title="Save the budget expense.">
        Save Expense
    </button>
</form>

.CS 。CS

public async Task<IActionResult> OnPostBudgetRevenueAsync()
{
    
}

public async Task<IActionResult> OnPostBudgetExpensesAsync()
{
    
}

The name of the page handler method is passed in the URL as a query string value named handler by default eg ?handler=BudgetRevenue and ?handler=BudgetExpenses - unless you have a "{handler?}" token in your route template for the page, in which case the name of the handler method becomes a segment in the URL: /BudgetRevenue or /BudgetExpenses .页面处理程序方法的名称在 URL 中作为名为handler的查询字符串值默认传递,例如?handler=BudgetRevenue?handler=BudgetExpenses - 除非您在页面的路由模板中有"{handler?}"标记,在这种情况下,处理程序方法的名称成为 URL 中的一个段: /BudgetRevenue/BudgetExpenses

So using jQuery, you pass that to the $.post method:因此,使用 jQuery,将其传递给$.post方法:

$('#btnSaveRevenue').on('click', function(event){
    event.preventDefault();
    $.post('?handler=BudgetRevenue', $('#editBudgetForm').serialize(), function(response){
        // do whatever
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM