.NET MVC第五章、模型绑定获取表单数据
2023-09-14 09:04:59 时间
.NET MVC第五章、模型绑定获取表单数据
目录
Html.ActionLink超链接
输出超链接使用的HTML辅助方法是Html.ActionLink,常见的有以下3种写法:
@Html.ActionLink("超链接1","函数名称")
@Html.ActionLink("超链接2","函数名称","控制器名称")
@Html.ActionLink("超链接3","函数名称", new { userName = "admin", age = 12 })
Html.ActionLink示例
控制器
public ActionResult Index(string userName,int? age=2)
{
ViewBag.userName = userName;
ViewBag.age = age;
return View();
}
视图层
@Html.ActionLink("超链接1", "Index")
<hr />
@Html.ActionLink("超链接2", "Index", "Test")
<hr />
@Html.ActionLink("超链接3", "Index", new { userName = "admin", age = 12 })
<hr/>
@ViewBag.userName
<br/>
@ViewBag.age
效果:
链接1、2直接访问,在url上可以看到,并且age的默认值是2,链接3显示admin与age的12,说明超链接符合预期。
HTML辅助方法生成表单
HTML辅助方法 | 说明 |
Html.BeginForm() | 输出<form>标签 |
Html.CheckBox() | 输出<input type="checkbox">标签 |
Html.DropDownList() | 输出<select>标签 |
Html.Password() | 输出<input type="password">标签 |
Html.RadioButton() | 输出<input type="radio">标签 |
Html.TextArea() | 输出<textarea/>标签 |
Html.TextBox() | 输出<input type="text">标签 |
Html.Hidden() | 输出<input type=”hidden”/>标签 |
示例
控制器
[HttpPost]
public ActionResult GetForm(string userName,string pwd,string sex,String []likes, string age,string introduce) {
string info = "";
info += userName+"<br/>";
info += pwd + "<br/>";
info += sex + "<br/>";
foreach (string l in likes) {
info += l + "<br/>";
}
info += age + "<br/>";
info += introduce + "<br/>";
TempData["show"] = info;
return Redirect("~/Test/Index");
}
视图层
@{
ViewBag.Title = "Index";
}
<h2>超链接</h2>
<hr />
<form action="~/Test/GetForm" method="post">
<fieldset>
<legend>表单</legend>
<hr />
<p>
@Html.TextBox("userName", "", new { @placeholder = "请输入用户名" })
</p>
<p>
@Html.TextBox("pwd", "", new { @placeholder = "请输入用密码" })
</p>
<p>
@Html.RadioButton("sex", "男") 男
@Html.RadioButton("sex", "女") 女
</p>
<p>
@Html.CheckBox("likes", "0") 健身
@Html.CheckBox("likes", "1") 交友
@Html.CheckBox("likes", "2") 蹦迪
</p>
<p>
@Html.DropDownList("age",
new List<SelectListItem> {
new SelectListItem { Text = "22岁", Value = "22" } ,
new SelectListItem { Text = "23岁", Value = "23" } ,
new SelectListItem { Text = "24岁", Value = "24" }
}
, new { @class = "form-control" })
</p>
<p>
@Html.TextArea("introduce")
</p>
<p>
<input type="submit" value="提交" class="btn btn-block btn-primary" />
</p>
</fieldset>
</form>
<hr />
@TempData["show"]
相关文章
- ASP.NET Core WebApi分布式文件系统FastDFS实战演练
- dbMigration .NET 数据同步迁移工具
- 如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
- 《ASP.NET1200例》ASP.Net 之Datalist数据删除(支持批量)
- ASP.NET使用ViewBag和HttpPost在MVC中进行基本数据类型的数据传递Demo
- ASP.NET MVC 数据传递进阶 从数据库拿到数据后的三种方式
- ASP.NET MVC 数据传递进阶 从数据库拿到数据后的三种方式
- ASP.NET MVC 数据传递 视图向控制器传递
- ASP.NET MVC 数据传递 控制器向视图传递
- Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]
- asp.net中Winform开发框架之数据即时更新的实现
- .net通过url访问服务器获取服务器返回数据
- 重新整理 .net core 实践篇————依赖注入应用之援军[四]
- .net通过url访问服务器获取服务器返回数据
- ASP.NET MVC 分部视图
- [ASP.NET Core 3框架揭秘] 配置[2]:读取配置数据[下篇]
- .NET平台机器学习组件-Infer.NET(三) Learner API—数据映射与序列化
- Atitit. .net c# web 跟客户端winform 的ui控件结构比较
- Word DocX 模板数据填充 .NET Word 报表
- TeeChart Pro for .NET 2022.10.24 Crack
- VB.net:VB.net编程语言学习之基于VS软件连接SQL Server(利用ADO.NET操作数据库/添加新数据源/DataGridView数据表格控件)的简介、案例应用之详细攻略
- 初识ASP.NET---若干常见错误
- ASP.NET Core微服务(七)——【docker部署linux上线】(RDS+API接口测试部分)