简介
Namespace: System.Web.Mvc.Html System.Web.Mvc.Html 命名空间包含有助于在 MVC 应用程序中呈现 HTML 控件的类。 该命名空间包含支持窗体、输入控件、链接、分部视图、验证等的类
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
html.BeginForm: 将 <form> 开始标记写入响应。 在用户提交窗体时,将由某个操作方法处理该请求。
BeginForm(HtmlHelper)
@using (Html.BeginForm())
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Original Controller/Original Action" action="post">
BeginForm(HtmlHelper, Object)
@<span style="color: blue;">using</span> (Html.BeginForm(<span style="color: blue;">new</span> { UserId = <span style="color: rgb(163, 21, 21);">"5"</span> }))
{
@Html.TextBox(<span style="color: rgb(163, 21, 21);">"Name"</span>);
@Html.Password(<span style="color: rgb(163, 21, 21);">"Password"</span>);
<input type=<span style="color: rgb(163, 21, 21);">"submit"</span> value=<span style="color: rgb(163, 21, 21);">"Sign In"</span>>
}
<span style="color: green;">// Produces the following form element</span>
<span style="color: green;">// <form action="/Original Controller/Original Action?UserId=5" action="post"></span>
BeginForm(HtmlHelper, RouteValueDictionary)
@{
var routeValues = new RouteValueDictionary();
routeValues.Add("UserId", "5");
}
@using (Html.BeginForm(routeValues))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Original Controller/Original Action?UserId=5" action="post">
BeginForm(HtmlHelper, String, String)
@{
<span style="color: blue;">var</span> routeValues = <span style="color: blue;">new</span> RouteValueDictionary();
routeValues.Add(<span style="color: rgb(163, 21, 21);">"UserId"</span>, <span style="color: rgb(163, 21, 21);">"5"</span>);
}
@<span style="color: blue;">using</span> (Html.BeginForm(routeValues))
{
@Html.TextBox(<span style="color: rgb(163, 21, 21);">"Name"</span>);
@Html.Password(<span style="color: rgb(163, 21, 21);">"Password"</span>);
<input type=<span style="color: rgb(163, 21, 21);">"submit"</span> value=<span style="color: rgb(163, 21, 21);">"Sign In"</span>>
}
<span style="color: green;">// Produces the following form element</span>
<span style="color: green;">// <form action="/Original Controller/Original Action?UserId=5" action="post"></span>
BeginForm(HtmlHelper, String, String)
</pre><pre class="html" name="code">@using (Html.BeginForm("Login", "Account"))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Account/Login" action="post">
BeginForm(HtmlHelper, String, String, FormMethod)
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Account/Login" action="post">
BeginForm(HtmlHelper, String, String, FormMethod, IDictionary<String, Object>)
@{
var attributes = new Dictionary<string, object>();
attributes.Add("Id", "Form1");
}
@using (Html.BeginForm("Login", "Account", FormMethod.Post, attributes))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form Id="Form1" action="/Account/Login" action="post">
BeginForm(HtmlHelper, String, String, FormMethod, Object)
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { Id = "Form1" }))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form Id="Form1" action="/Account/Login" action="post">
BeginForm(HtmlHelper, String, String, Object)
@using (Html.BeginForm("Login", "Account", new { UserId = "5" }))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Account/Login?UserId=5" action="post">
BeginForm(HtmlHelper, String, String, Object, FormMethod)
@using (Html.BeginForm("Login", "Account", new { UserId = "5" }, FormMethod.Post))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Account/Login?UserId=5" action="post">
BeginForm(HtmlHelper, String, String, Object, FormMethod, Object)
@using (Html.BeginForm("Login", "Account", new { UserId = "5" }, FormMethod.Post, new { Id = "Form1" }))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form Id="Form1" action="/Account/Login?UserId=5" action="post">
BeginForm(HtmlHelper, String, String, RouteValueDictionary)
@{
var routeValues = new RouteValueDictionary();
routeValues.Add("UserId", "5");
}
@using (Html.BeginForm("Login", "Account", routeValues))
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
// Produces the following form element
// <form action="/Account/Login?UserId=5" action="post">
BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod)
BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod, IDictionary<String, Object>)
访问form元素
- 如果在当前page中只有一个form:
// find forms in the page
$("form")
- html.BeginForm 构造函数传入 form Id。如:
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { Id = "Form1" })){ //element}
@using (Html.BeginForm("Login", "Account", new { UserId = "5" }, FormMethod.Post, new { Id = "Form1" }))
访问form:
// find the form by id
$("#myForm1");