Html.BeginForm() 是否必要?

发布于 2024-08-05 06:28:10 字数 49 浏览 1 评论 0原文

Html.BeginForm() 的作用是什么?有必要吗?

What does Html.BeginForm() do and is it necessary?

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

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

发布评论

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

评论(3

只是一片海 2024-08-12 06:28:11

本质上,它将一个

<form>

标签输出到 HTML 输出中。如果您的页面进行 POST(即按下按钮来提交表单),则需要表单标记,以便浏览器知道在哪里提交表单。
它现在可能对您有用的原因是因为您可能没有任何按钮 - 只是链接(我不知道您的应用程序是什么,所以只是在这里猜测)。

简而言之:如果您希望能够向操作提交表单,是的,该标签是必需的。如果您通过链接完成这一切,则不需要表单标记,因此实际上并不需要 BeginForm。

HTH。

PS 阅读了 Scott Guthrie 关于 ASP.NET MVC 的博客文章,它们确实对您入门有很大帮助。

Essentially, it outputs a

<form>

tag into the HTML output. The form tag is required if your page POSTs (i.e. a button is pressed to submit the form), so that the browser knows where to submit the form.
The reason it might be working right now for you, is because you might not have any buttons - jsut links (I dont know what your application is, so just guessing here).

In short: if you want to be able to submit a form to an action, yes, that tag is required. If you do it all through links, you dont need a form tag, and thus BeginForm is not really needed.

HTH.

P.S. read Scott Guthrie's blog posts on ASP.NET MVC, they really help a lot to get you started.

暮凉 2024-08-12 06:28:11

BeginForm() 只是使用提供给它的参数写出表单标签。如果您没有表单,或者如果您使用 AJAX 进行所有页面交互,则可能不需要它。如果页面上只有链接,则不需要表单。不过,除非您使用 AJAX,否则您无法在不使用控制器操作注入表单标签或手动注入表单标签的情况下对控制器操作执行 POST 请求。

BeginForm() simply writes out the form tag using the parameters supplied to it. If you don't have a form you won't or if you're doing all of your page interaction with AJAX you might not need it. If you only have links on the page, then a form is unnecessary. Unless you use AJAX, though, you can't do POST requests to your controller actions without using it to inject your form tag or injecting your form tag manually.

内心荒芜 2024-08-12 06:28:11

根本没有必要。它只是一个处理设置表单标签的助手。不过,使用它是个好主意。我的直觉表明,他们将增强这个帮助程序来处理 XSS 攻击等问题,您目前必须使用 AntiForgeryToken 帮助程序和相关属性来处理这些问题。

It's not necessary at all. It's simply a helper which handles setting up the form tags. It's a good idea to use it though. My intuition says they will enhance this helper to handle things like XSS attacks etc. that you currently have to deal with using the AntiForgeryToken helper and associated attribute.

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