在 ASP.NET Core 中,我可以为链接项 a
设置一个 Action ,但是如果我将它更改为一个按钮,它就不再起作用了。
将操作/ Controller 绑定(bind)到按钮
点击的正确方法是什么?
<a asp-action="Delete" asp-route-id="@item.Id">remove</a> @*work*@
对比
<button asp-action="Delete" asp-route-id="@item.Id">remove</button> @*does not work*@
最佳答案
不是不行,它们是不同的Tag Helpers,用法不同:
如果将鼠标悬停在 <a>
上你会看到它实现了 AnchorTagHelper
, 而 <button>
实现 FormActionTagHelper
,因为它应该用于 <form>
因此,为了获得相同的行为,您可以这样做:
<a asp-action="Index">aaaa</a>
<form>
<button asp-action="Index">bbbbb</button>
</form>
不过请注意,该按钮呈现为 formaction="/"
, 不像 href="/"
,这就是为什么您需要将其包装在一个表单中。
第二个也可以这样写:
<form asp-action="Index">
<button type="submit">bbbbb</button>
</form>
关于c# - 让标签助手像 "button"一样工作 "a",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592268/