MVC HtmlHelper用法(一)@Html.BeginForm的使用总结
1.@using(Html.BeginForm()){} //提交到当前页面
2.@using(Html.BeginForm( new {} )){} //提交到当前页面,new中可以带参数
3.@using(Html.BeginForm("action","controller")){} //提交到指定controller下
4.@using(Html.BeginForm("action","controller",FormMethod.Post)) //指定提交方式
注:所有要提交的内容包括按钮都必须在{ }内
example:
1.指定表单提交路径和方式:
@using(Html.BeginForm("Index","Home",FormMethod.Get,new{ name = "nbForm",id="idForm"})){}
html格式:
<form name="nbForm" id="idForm" action="/Home/Index" method="get"> </form>
2.指定表单提交方式为文件方式:
@using(Html.BeginForm("ImportExcel","Home",FormMehod.Post,new { enctype="multipart/form-data"})){}
html格式:略
注意, 有时候要加{id=1}不然在点击超过第一页的索引时form后面会自动加上当前页的索引,如果此时再搜索则可能会出来“超出索引值”的错误提示
@using (Html.BeginForm("Index", null, new { id = 1 }, FormMethod.Get))
3.添加new { area = string.Empty }是为了防止提交链接后面自带参数,方式自定,如可写成 new { id = ""}
@using (Html.BeginForm("Shipping", "Order", new { area = string.Empty }, FormMethod.Post, new { id = "ShippingForm" }))
4.@using(Html.BeginForm("Index","Home",FormMehod.method)){}也可以写作
<% using(Html.BeginForm("Index","Home",FormMethod.method)){%>
<%}%>
FormMethod为枚举方式
public enum FormMethod
{
Get=0,
Post=1,
}
5.控制器接受参数的方式
ex:在aspx中
@using(Html.BeginForm("Index","Home",FormMehod.Post))
{
@Html.TextBox("username")
@Html.TextBox("password")
<input type="submit" value="登陆"/>
}
控制器中
public ActionResult Index()
{
string struser=Request.Form["username"];
string strpass=Request.From["password"];
ViewData["v"]="你的账号是"+struser+"密码是"+strpass;
return View();
}
在Index.aspx中接受传值
<%=ViewData["v"]%>
ex:在MVC中
@using (Html.BeginForm("Shipping", "Order", new { area = string.Empty }, FormMethod.Post, new { id = "ShippingForm" }))
{
@Html.ValidationMessage("ShipInfo.DeliverType")
@Html.HiddenFor(m => m.ShipInfo.DeliverCountry)
@Html.HiddenFor(m => m.ShipInfo.DeliverProvince)
@Html.HiddenFor(m => m.ShipInfo.DeliverCity)
@Html.HiddenFor(m => m.ShipInfo.DeliverCityArea)
}
1. HTML标签name 和参数名一样
public ActionResult Shipping(string ShipInfo.DeliverType,string ShipInfo.DeliverCountry,string ShipInfo.DeliverProvince,string ShipInfo.DeliverCity,string ShipInfo.DeliverCityArea){}
2.HTML标签name 属性和Model属性保持一致
public ActionResult Shipping(Models.ViewModels.Order.OrderViewModel model){}
3.表单集合传参
public ActionResult Shipping( FormCollection fc)
{
string strResult=fc["selectOption"]; //集合传参要以 键值对的方式 获得数据
}
最新文章
- Redmi Note3 hennessy 刷机过程记录
- [JS] JavaScript由浅入深(2) 进阶
- css清楚浮动的几种常用方法
- iOS部分其他知识
- C语言进行CGI程序设计
- HelloWorld——Cocos2d-x学习历程(二)
- ftp一些东东
- mysql版sql助记
- python web开发之django
- 201521123065《java程序设计》第12周学习总结
- [转帖]Kerberos和NTLM - SQL Server
- 小程序开发------mpvue开发时间轴
- Fragment的常用写法
- ​0​天​掌​握​i​O​S​开​发​之​D​a​y​2​ ​-​ ​内​存​管​理 (给学生讲解的课件,总结的不错)
- QtCreator 可以通过 Clang-Tidy 和 CLazy 对你的代码进行静态检查
- ethers.js-3-Providers
- Ubuntu 10.04 安装流程
- Python之数据库导入(py3.5)
- rbac——界面、权限
- oozie调用java实例------shell action
热门文章
- eclipse安装color theme插件
- error===>;ld: 2 duplicate symbols for architecture x86_64
- Java_Java Compiler 应用实例
- SVN文本文件报二进制属性的问题
- OpenCV 3.1 StereoBM 获取正确视差Dispariy
- 【HDU4632 Palindrome subsequence】区间dp
- html只允许输入的数据校验,只允许输入字母汉字数字等
- Oracle中有个tkprof来格式化oracle的trace文件
- 20145337《Java程序设计》第四周学习总结
- Wcf 双工通信的应用