我如何根据单击的按钮 post 向两个不同的处理程序发送相同的数据?
How can I post the same data to two different handlers depending on the button clicked?
[见底部更新]
我有一个带有表单的 Razor 页面。我想在该表单上有两个按钮,它们执行的操作略有不同 - 都使用相同的发布表单数据。
我尝试在第二个按钮上使用 asp-page-handler
助手,但它似乎没有向 HTML 添加任何内容(我希望它添加 formaction
属性到 <button>
元素,但它根本不添加任何内容)。
这是一个示例页面:
@page "{id?}"
@model IndexModel
@tagHelperPrefix x:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<p>Current value is @Model.Foo</p>
<x:form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" x:asp-page-handler="Alternative">Alternative</button>
</x:form>
...这是相应的页面模型:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace MyWebApplication.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public string Foo { get; set; }
public void OnGet(int? id)
{
}
public void OnPostAsync(string foo)
{
Foo = foo;
}
public void OnPostAlternativeAsync(string foo)
{
Foo = foo.ToUpper();
}
}
}
这呈现为:
...其中为表单生成的 HTML 是:
<form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" x:asp-page-handler="Alternative">Alternative</button>
</form>
x:asp-page-handler
属性仍在生成的 HTML 中这一事实让我认为 Razor 引擎尚未识别它。我试过去掉 x:
前缀,但没有用。
我做错了什么?
更新
好的,我尝试删除标签前缀 和 删除 @tagHelperPrefix
行,结果有所不同。 formaction
按预期添加到第二个 <button>
元素。
但是:
- 这真的很烦人 -
@tagHelperPrefix
不是我想要失去的东西,
- 现在 两个 按钮都触发了 "Alternative" 操作,即使其中只有一个按钮具有
formaction
!
这是新生成的 HTML:
<form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" formaction="/?handler=Alternative">Alternative</button>
</form>
第二次更新
好的,所以如果我将 asp-page-handler=""
放在 "default" 按钮上,那么每个按钮都会转到正确的处理程序,这很好。
剩下的最后一个问题是:我怎样才能使用标签助手前缀来完成这项工作?
[回答我自己的问题以防对其他人有帮助。]
事实证明:
- tag-helper-prefix 仅适用于 elements,不适用于 attributes,因此它应该是
asp-page-handler="..."
而不是x:asp-page-handler="..."
即使标签助手前缀是 x:
.
- 这些
asp-
属性只能在启用标签助手的标签中识别 - 当没有指定标签助手前缀时,即 all 元素,或者只有指定了 tag-helper-prefix 的元素。就我而言,我不得不将 <button ...>
更改为 <x:button ...>
。
- 如果为一个按钮指定
asp-page-handler
,则需要在所有按钮上指定它,即使将其指定为""
也可以得到默认操作。
[见底部更新]
我有一个带有表单的 Razor 页面。我想在该表单上有两个按钮,它们执行的操作略有不同 - 都使用相同的发布表单数据。
我尝试在第二个按钮上使用 asp-page-handler
助手,但它似乎没有向 HTML 添加任何内容(我希望它添加 formaction
属性到 <button>
元素,但它根本不添加任何内容)。
这是一个示例页面:
@page "{id?}"
@model IndexModel
@tagHelperPrefix x:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<p>Current value is @Model.Foo</p>
<x:form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" x:asp-page-handler="Alternative">Alternative</button>
</x:form>
...这是相应的页面模型:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace MyWebApplication.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public string Foo { get; set; }
public void OnGet(int? id)
{
}
public void OnPostAsync(string foo)
{
Foo = foo;
}
public void OnPostAlternativeAsync(string foo)
{
Foo = foo.ToUpper();
}
}
}
这呈现为:
...其中为表单生成的 HTML 是:
<form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" x:asp-page-handler="Alternative">Alternative</button>
</form>
x:asp-page-handler
属性仍在生成的 HTML 中这一事实让我认为 Razor 引擎尚未识别它。我试过去掉 x:
前缀,但没有用。
我做错了什么?
更新
好的,我尝试删除标签前缀 和 删除 @tagHelperPrefix
行,结果有所不同。 formaction
按预期添加到第二个 <button>
元素。
但是:
- 这真的很烦人 -
@tagHelperPrefix
不是我想要失去的东西, - 现在 两个 按钮都触发了 "Alternative" 操作,即使其中只有一个按钮具有
formaction
!
这是新生成的 HTML:
<form method="post">
<input type="text" name="foo" />
<button type="submit">Default</button>
<button type="submit" formaction="/?handler=Alternative">Alternative</button>
</form>
第二次更新
好的,所以如果我将 asp-page-handler=""
放在 "default" 按钮上,那么每个按钮都会转到正确的处理程序,这很好。
剩下的最后一个问题是:我怎样才能使用标签助手前缀来完成这项工作?
[回答我自己的问题以防对其他人有帮助。]
事实证明:
- tag-helper-prefix 仅适用于 elements,不适用于 attributes,因此它应该是
asp-page-handler="..."
而不是x:asp-page-handler="..."
即使标签助手前缀是x:
. - 这些
asp-
属性只能在启用标签助手的标签中识别 - 当没有指定标签助手前缀时,即 all 元素,或者只有指定了 tag-helper-prefix 的元素。就我而言,我不得不将<button ...>
更改为<x:button ...>
。 - 如果为一个按钮指定
asp-page-handler
,则需要在所有按钮上指定它,即使将其指定为""
也可以得到默认操作。