asp.net-mvc MVC3中Html.BeginForm有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11644968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:07:22  来源:igfitidea点击:

What is the use of Html.BeginForm in MVC3

asp.net-mvc

提问by Charu

What is the use of Html.BeginForm in MVC3. Why do we use it, when we can just add a form tag directly, does this html helper add some capability or does something which cannot be done with a simple form tag.

MVC3中Html.BeginForm有什么用。我们为什么要使用它,当我们可以直接添加表单标签时,这个html帮助器是否添加了一些功能或做一些简单的表单标签无法完成的事情。

回答by Jesse Hallam

The Html.BeginFormhelper method contains a couple overloadswhose intended purpose is to make writing routed forms easier. It is aware of MVC stucture and makes sure its targeting a controller and action. It's just a bit of syntactic sugar over:

Html.BeginForm辅助方法,包含了几个重载,其使用目的是使编写路由形式更容易。它了解 MVC 结构并确保其针对控制器和操作。这只是一些语法糖:

<form method="post" action="@Url.Action(...)">

In Microsoft's words:

用微软的话来说:

The ASP.NET MVC framework includes helper methods that provide an easy way to render HTML in a view.

ASP.NET MVC 框架包括辅助方法,这些方法提供了一种在视图中呈现 HTML 的简单方法。

Of course, no one is making you use them. Its just a matter of preference. In fact, in the early days of MVC, many WebForms developers celebrated their new freedom from server controls a-la <asp:TextBox>et al., and insisted on writing everything by hand.

当然,没有人让你使用它们。它只是一个偏好问题。事实上,在 MVC 的早期,许多 WebForms 开发人员庆祝他们摆脱了服务器控件 a-la<asp:TextBox>等人的新自由,并坚持手工编写所有内容。

Using the helpers for your form fields comes highly recommended, since they're aware of things like form validation. Html.BeginFormjust gives you a consistent way to start and finish your form:

强烈建议为您的表单字段使用助手,因为它们知道表单验证之类的事情。Html.BeginForm只是给你一个一致的方式来开始和完成你的表格:

@using(Html.BeginForm())
{
    @Html.LabelFor(...)
    @Html.EditorFor(...)
}

Html.BeginFormreturns an IDisposableobject, allowing you to wrap it in the C# usingstatement. When the usingexits, the disposal will call Html.EndForm()automatically for you. Since Html.EndFormreturns voidit is slightly inconvenient to call from Razor:

Html.BeginForm返回一个IDisposable对象,允许您将其包装在 C#using语句中。当using退出时,处置会Html.EndForm()自动呼叫您。由于Html.EndForm返回void,从 Razor 调用有点不方便:

@Html.BeginForm()
<formStuff>
@{Html.EndForm();}

A simple @Html.EndForm()will turn in to Write(Html.EndForm()) -> Write(void), ie compile time error.

一个简单的@Html.EndForm()就会变成Write(Html.EndForm()) -> Write(void),即编译时错误。

回答by Lucero

Of course you can code it by hand, but it does have several advantages, such as:

当然,您可以手动编写代码,但它确实有几个优点,例如:

  • it returns an object that is disposable, so that you can put it in a usingclause and it will close the form tag for you
  • it computes the URL for the form action
  • 它返回一个一次性的对象,这样你就可以把它放在一个using子句中,它会为你关闭表单标签
  • 它计算表单操作的 URL

回答by darkey

does something which cannot be done with a simple form

做一些简单的表格无法完成的事情

At the end every helper method call is converted to pure HTML so there is nothing that Html.BeginForm can do that can't be done by using the <form>tag directly.

最后,每个辅助方法调用都转换为纯 HTML,因此 Html.BeginForm 可以做的任何事情都不能通过<form>直接使用标记来完成。

Html.BeginFormis purely an helper method.

Html.BeginForm纯粹是一个辅助方法。

回答by Dmitry Efimenko

What's the purpose of having helpers at all? (Rhetorical question) You could type all inputs manually as well, but helpers... well, help you sometimes.

有帮手的目的是什么?(反问)您也可以手动输入所有输入,但是帮手……好吧,有时会帮助您。

all it does it put tag. Yes, you can do it manually, or you can be a bit more fancy and do this:

它所做的一切都贴上了标签。是的,您可以手动完成,或者您可以更花哨地执行以下操作:

@using(Html.BeginForm())
{
}

This will close the form tag for you as well. So if you keep all your inputs inside the curvy brackets, you don't need to worry about remembering closing the form tag.

这也将为您关闭表单标签。因此,如果您将所有输入保留在大括号内,则无需担心记住关闭表单标签。

回答by John

Excellent question, I wondered about that myself quite a bit.

很好的问题,我自己也很想知道这个问题。

I could be wrong, but here's my guess (a better man may correct me):

我可能是错的,但这是我的猜测(更好的人可能会纠正我):

In the early days of MVC, there was no razor.

在 MVC 的早期,没有剃刀。

Switching between C#/VB and html was hard syntax-wise, so there was a push towards minimizing those borders.

在 C#/VB 和 html 之间切换在语法上是很困难的,因此推动了这些边界的最小化。

When creating forms, it was seen beneficial to create the entire form purely on the C#/VB side, without any intermingling manual html, to just have one border to the surrounding world of html.

在创建表单时,纯粹在 C#/VB 端创建整个表单是有益的,没有任何混合的手动 html,只有一个边界到周围的 html 世界。

And since programmers often copy the style of doing things from somewhere else, the practice of using those helpers has persisted even though their benefit has disappeared with the advent of razor.

而且由于程序员经常从其他地方复制做事的风格,使用这些助手的做法仍然存在,即使它们的好处随着剃刀的出现而消失。