什么是MVC3使用Html.BeginForm的 [英] What is the use of Html.BeginForm in MVC3
问题描述
什么是MVC3使用Html.BeginForm的。
我们为什么要使用它,我们可以只直接添加一个表单标签,这是否HTML辅助添加一些功能或做一些事情,不能用一个简单的表单标签来完成。
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.
推荐答案
的 Html.BeginForm
辅助方法,包含一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform.aspx\">couple重载的预期,其目的是为了使写作路由形式更容易。它知道MVC stucture,并确保其定位控制器和动作。这只是一个有点语法糖过来:
The Html.BeginForm
helper method contains a couple overloads whose 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:
<form method="post" action="@Url.Action(...)">
在微软的话说:
ASP.NET MVC框架包括辅助方法提供了一种方便的方法来呈现HTML视图中。
The ASP.NET MVC framework includes helper methods that provide an easy way to render HTML in a view.
当然,没有人让你使用它们。它只是preference的问题。事实上,在MVC的初期,许多的WebForms开发商庆祝从服务器的新的自由控制一拉&LT; ASP:文本框&GT;
等,并在写作坚持用手一切。
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.
值得推荐,因为他们是知道的东西像表单验证。 Html.BeginForm
只是给你一个一致的方式来启动和完成表单:
Using the helpers for your form fields comes highly recommended, since they're aware of things like form validation. Html.BeginForm
just gives you a consistent way to start and finish your form:
@using(Html.BeginForm())
{
@Html.LabelFor(...)
@Html.EditorFor(...)
}
Html.BeginForm
返回的IDisposable
的对象,让你敷在C#使用
语句。当使用
退出,处置将调用 Html.EndForm()
自动为您。由于 Html.EndForm
收益无效
是稍显不便,从剃刀拨打:
Html.BeginForm
returns an IDisposable
object, allowing you to wrap it in the C# using
statement. When the using
exits, the disposal will call Html.EndForm()
automatically for you. Since Html.EndForm
returns void
it is slightly inconvenient to call from Razor:
@Html.BeginForm()
<formStuff>
@{Html.EndForm();}
一个简单的 @ Html.EndForm()
将变成写(Html.EndForm()) - GT;写(无效)
,即编译时错误。
A simple @Html.EndForm()
will turn in to Write(Html.EndForm()) -> Write(void)
, ie compile time error.
这篇关于什么是MVC3使用Html.BeginForm的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!