Html.BeginForm())与Ajax.BeginForm()
一、@using (Html.BeginForm())参数示例
1、指定表单提交方式和路径等
@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { name = "nbform", id = "nbform" }))
2、指定表单提交为数据方式
@using (Html.BeginForm("ImportExcel", "Stock", FormMethod.Post, new { enctype = "multipart/form-data" }))
注意, 有时候要加{id=1}不然在点击超过第一页的索引时form后面会自动加上当前页的索引,如果此时再搜索则可能会出来“超出索引值”的错误提示
@using (Html.BeginForm("Index", null, new { id = 1 }, FormMethod.Get))
3、下面的操作可以防止提交链接后面自带参数
@using (Html.BeginForm("AddDIYBillOfLading", "BillOfLading", new { id ="" }, FormMethod.Post, new { name = "myform", id = "myform" }))
即,RouteValues的id=""
二、Ajax.BeginForm()
@using (Ajax.BeginForm("Search", new RouteValueDictionary { { "id", "" } },
new AjaxOptions { UpdateTargetId = "ListData", HttpMethod = "Get", InsertionMode = InsertionMode.Replace },
new RouteValueDictionary { { "id", "searchForm" } }))
{……}
注释:创建了一个ajax的表单,其中动作是Search,参数id是"",http请求的方法是Get,插入方式是替换(如果你做的是瀑布流,还可以用追加),这个表单本身的id是searchForm
Ajax.BeginForm异步表单原型解释
1 @using (Ajax.BeginForm( 2 new AjaxOptions 3 { 4 UpdateTargetId = "UserLogOnContainer", 5 HttpMethod = "Post", 6 OnSuccess = " ", 7 })){} //提交到当前页面,提交方式为Post,异步更新模块ID为UserLogOnContainer 8 9 @using (Ajax.BeginForm("action", "controller", null, 10 new AjaxOptions 11 { 12 UpdateTargetId = "UserLogOnContainer", 13 HttpMethod = "Post", 14 OnSuccess = " ", 15 })) 16 {} //提交到指定controller下的action,提交方式为Post,异步更新模块ID为UserLogOnContainer
表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;
只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作.