ASP.NET4.5MVC实战教程5.表单视图实战1

上传人:自*** 文档编号:54259417 上传时间:2018-09-10 格式:PPT 页数:26 大小:1.45MB
返回 下载 相关 举报
ASP.NET4.5MVC实战教程5.表单视图实战1_第1页
第1页 / 共26页
ASP.NET4.5MVC实战教程5.表单视图实战1_第2页
第2页 / 共26页
ASP.NET4.5MVC实战教程5.表单视图实战1_第3页
第3页 / 共26页
ASP.NET4.5MVC实战教程5.表单视图实战1_第4页
第4页 / 共26页
ASP.NET4.5MVC实战教程5.表单视图实战1_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《ASP.NET4.5MVC实战教程5.表单视图实战1》由会员分享,可在线阅读,更多相关《ASP.NET4.5MVC实战教程5.表单视图实战1(26页珍藏版)》请在金锄头文库上搜索。

1、BF-TECH 4.0 DNET 软件开发工程师高薪就业品牌课程 版权所有:北风网,ASP.NET 4.5 MVC 开发实战教程 讲师:石曼迪,第三章:表单及基本验证,目录,构建表单 处理表单,构建表单,ASP.NET MVC虽然鼓励我们手写HTML代码,但是同时也还是提供了很多Hellper的方法,Hellper就是一些生成HTML代码的方法,方便我们书写HTML代码。如: Html.LabelFor Html.TextBoxFor Html.PasswordFor ,构建表单,ASP.NET MVC虽然鼓励我们手写HTML代码,但是同时也还是提供了很多Hellper的方法,Hellper就

2、是一些生成HTML代码的方法,方便我们书写HTML代码。如: Html.LabelFor Html.TextBoxFor Html.PasswordFor ,构建表单:ActionLink&RouteLink,Html.ActionLink(“连接“, “Index“, “Home“) ActionLink中的三个参数分别为:显示的文字,Action,Controller,Html.ActionLink(“连接“, “Index“, “Home“, new page=1 ,null)Html.ActionLink(“连接“, “Index“, new page=1 )Html.ActionLi

3、nk(“连接“, “Index“, “Home“, new id=“link1“ )Html.ActionLink(“连接“, “Index“,null, new id=“link1“ ) Html.ActionLink(“连接“, “Index“, “Home“, new page = 1 , new id = “link1“ )Html.ActionLink(“连接“, “Index“ , new page = 1 , new id = “link1“ ),连接 连接 连接 连接 连接 连接,注意,如果连接中不涉及到action及controller就没有必要使用ActionLink,而是

4、直接写HTML代码就可以了。 例如:一章,构建表单:ActionLink&RouteLink,如何给Html.ActionLink添加删除确认?,Html.ActionLink(“删除“, “Delete“, new id = item.Id , new onclick = “return confirm(确认删除?)“ ),删除,构建表单:Form,using (Html.BeginForm() Html.BeginForm(“index“, “home“, FormMethod.Post)Html.EndForm() 生成结果:,构建表单:Form,在一个FORM中有两个或多个submit

5、按钮(比如一个登录按钮,一个注册按钮当post出去后,在controller如何区分是那个按钮被按下?,if (collection“SubmitToDB“ != null)/点了loginif (collection“Upload“ != null)/ 点了reg,构建表单:TextBox&Hidden&Label,文本输入框和隐藏域,Html.TextBox(“input1“) Html.TextBox(“input2“,Model.CategoryName,new style = “width:300px;“ ) Html.TextBox(“input3“, ViewData“Name“

6、,new style = “width:300px;“ ) Html.TextBoxFor(a = a.CategoryName, new style = “width:300px;“ ) Html.Hidden(“hideTag“,“hideValue“),构建表单:TextArea,多行文本框,Html.TextArea(“input5“, Model.CategoryName, 3, 9,null) Html.TextAreaFor(a = a.CategoryName, 3, 3, null),Beverages Beverages,构建表单:CheckBox,复选框,Html.Che

7、ckBox(“chk1“,true) Html.CheckBox(“chk1“, new class=“checkBox“) Html.CheckBoxFor(a =a.IsVaild, new class = “checkBox“ ),构建表单:CheckBox,怎么获取前端selected的值,using (Html.BeginForm() foreach (MembershipUser user in Model.Users) user.UserName ,public ActionResult Index(string checkedUsers) /这里自动获取选中的checkedUs

8、ers foreach (Guid id in checkedUsers) DeleteUser(id); ,构建表单:DropDownList,构建下拉选项,Html.DropDownList(“ddl1“, (SelectList)ViewData“Categories“, “-Select One-“) Html.DropDownList(“Categories“)model MvcApplication1.Models.LoginModel Html.DropDownListFor(m=m.UserName, (SelectList)ViewData“Categories2“, “-S

9、elect One-“, new class = “dropdownlist“ ),Dictionary strss = new Dictionary();strss.Add(2, “China“);strss.Add(5, “DE“);strss.Add(7, “US“);ViewBag.jihes = strss;ViewData“Categories“ = new SelectList(strss,“Key“,“Value“);,List logins = new List new LoginModel Password = “123456“, RememberMe = true, Us

10、erName = “admin“ ,new LoginModel Password = “123456“, RememberMe = true, UserName = “admin“ ;ViewData“Categories2“ = new SelectList(logins, “Password“, “UserName“);,构建表单:DropDownList,-Select One- China DE USChina DE US-Select One- admin admin ,构建表单:RadioButton,Html.RadioButton(名称,值,是否选中) 如: 组一:Html.

11、RadioButton(“name“,“男“,true) Html.RadioButton(“name“,“女“)组二: Html.RadioButton(“name2“,1,true) Html.RadioButton(“name2“,2) Html.RadioButton(“name2“,3),构建表单进阶,创建特性定制元数据,构建表单进阶,实现IMetadataAware接口的特性定制Model元数据 如果需要处理本地化资源,比如多语言,一般推荐使用资源文件方式。 实现步骤: 定义接口IMetadataAware实现类DisplayTextAttribute ,再定义DisplayNam

12、e和ResourceType冰实现接口方法。 新建资源文件。 添加DisplayText特性* 用到一部分预定义模板,构建表单高级,自定义模板步骤: 1.在Views/Shared/EditorTemplates下新建一个名为Boolean.cshtml的分部视图 2. 然后返回该自定义对象 3.启用模型编辑模式,构建表单高级,举例:复杂自定义模板将枚举以下拉框的形式显示,处理表单,Html.LabelFor(m = m.UserName) Html.TextBoxFor(m = m.UserName) Html.CheckBoxFor(m = m.RememberMe),public Act

13、ionResult Login(LoginModel model) Request.Form“name“; public ActionResult Login(FormCollection formCollection),用户名 密码 记住我?,使用建议,View负责输出数据而非样式(尽量用helper,不要写html代码)由于测试困难,尽量少在View中写代码 Return View(“View的名字”),如果不指定名字则返回与Controller同名的View Return View(“/url路径“),指定返回路径,现场编程,完成一个基本登录/注册功能 包含基本权限验证 基本数据库访问,总结,构建表单 处理表单,欢迎访问我们的官方网站 ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 教学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号