1.重载方法:
名称 | 说明 |
---|---|
BeginForm(HtmlHelper) | 将<form> 开始标记写入响应。 在用户提交窗体时,将由某个操作方法处理该请求。 |
BeginForm(HtmlHelper, Object) | 同上 |
BeginForm(HtmlHelper, RouteValueDictionary) | 同上 |
BeginForm(HtmlHelper, Object) | 同上 |
BeginForm(HtmlHelper, RouteValueDictionary) | 同上 |
BeginForm(HtmlHelper, String, String) | 同上 |
BeginForm(HtmlHelper, String, String, Object) | 同上 |
BeginForm(HtmlHelper, String, String, FormMethod) | 同上 |
BeginForm(HtmlHelper, String, String, RouteValueDictionary) | 同上 |
BeginForm(HtmlHelper, String, String, Object, FormMethod) | 同上 |
BeginForm(HtmlHelper, String, String, FormMethod, IDictionary<String, Object>) | 同上 |
BeginForm(HtmlHelper, String, String, FormMethod, Object) | 同上 |
BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod) | 同上 |
BeginForm(HtmlHelper, String, String, Object, FormMethod, Object) | 同上 |
BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod, IDictionary<String, Object>) | 同上 |
2.使用示例:
@using (Html.BeginForm()){
@Html.TextBox("search")
<input type="submit" />
}
这是最简单的一种,点击提交以后,这个表单会以Post方式提交到View对应的Action中去。
@using (Html.BeginForm("Result", "Search"))
{
@Html.TextBox("search")
<input type="submit" />
}
这种是提交到名字为Search的Controller的Result中去,默认的提交方式是Post。当然,也可以改成Get,方法如下:
@using (Html.BeginForm("Result", "Search", FormMethod.Get))
{
@Html.TextBox("search")
<input type="submit" />
}
小编Ps:这种用using BeginForm的方式是最常见的,还有另一种是EndForm的形势,读者可以自行了解。有一种比较特殊的BeginForm的参数,在上传文件的时候,其中的参数要设置成这样:
@using (Html.BeginForm("您的Action", "您的Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="photo" name="photo" />
<input type="submit" value="上传文件" />
}
注意:如果不这样设置有可能获取不到文件!