使用锚标记的ASP.net核心像表单一样工作 [英] ASP.net core using an anchor tag to work like a form

查看:75
本文介绍了使用锚标记的ASP.net核心像表单一样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net核心剃刀引擎.有没有一种方法可以像表单标签一样获取锚标签来访问我的Create方法.我使用表单标签测试了我的代码,它可以正常工作,有没有办法让锚标签做同样的事情?

I am using asp.net core razor engine. Is there a way to get an anchor tag to access my Create method like a form tag does. I tested my code using a form tag and it works, is there a way to have the anchor tag do the same?

这是我的代码

<div id = "menu">
    @model ecommerce.Models.Users
    <a asp-controller="Product" asp-action="Create" method="post" role="form">Test</a>  // This is the anchor tag that I want to have the same behavior as the form tag below

    <form asp-controller="Product" asp-action="Create" method="post" role="form">

    <button type="submit">Add to Quotes</button>
</form>
</div>

推荐答案

您面临的问题是锚标记将创建 GET 请求,而表单发布将是 POST请求.这就是为什么您的锚点不会点击与表单发布相同的create方法的原因.

The problem you are facing is that an anchor tag will create a GET request and a form post will be a POST request. That's why your anchor will not hit the same create method as your form post.

如果您只想在表单发布后重定向用户,则只需执行常规的表单发布并在create方法的末尾重定向他们即可.这是很常见的事情.

If you just want to redirect the user after the form post then just do a normal form post and redirect them at the end of the create method. This is a very common thing.

如果您确实希望锚点指向create方法,请创建该方法的第二个版本,即 GET 并将锚点标签指向该版本.

If you really want the anchor to point to a create method then create a second version of the method which is a GET and point the anchor tag there.

这篇关于使用锚标记的ASP.net核心像表单一样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆