authour: | chenboyi |
updatetime: | 2015-04-27 21:57:17 |
friendly link: |
目录:
//HtmlHelperController.cs
1 using MVC知识点.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7 8 namespace MVC知识点.Controllers 9 { 10 /// <summary> 11 /// 负责演示@Html 中的扩展方法,具体请看 Index.cshtml视图 12 /// </summary> 13 public class C03HtmlHelperController : Controller 14 { 15 // 16 // GET: /C03HtmlHelper/ 17 public ActionResult Edit() 18 { 19 Dog dog = new Dog() 20 { 21 Name = "小黄" 22 , 23 Age = 2, 24 Gender = false, TypeID=2 25 }; 26 27 List<Types> tlist = new List<Types>() { 28 new Types(){ TName="类型1", Tid=1}, 29 new Types(){ TName="类型2", Tid=2} 30 }; 31 /* 32 * SelectList:作用配合@Html.DropDownlist() @Html.DropDownlistFor() 两个使用的 33 * 第1个参数:List集合 34 * 第2个参数:生成到option中的value中的属性名称 35 * 第3个参数:生成到option中的text中的属性名称 36 * 第4个参数:指定要从绑定后的所有option中选择的对象项,和option中的value值进行比对 37 */ 38 //SelectList list = new SelectList(tlist, "Tid", "TName", 2); 配合@Html.DropDownlist() 方法的默认选择值 39 SelectList list = new SelectList(tlist, "Tid", "TName"); 40 41 //将list传入视图edit.cshtml 42 ViewBag.tlist = list; 43 44 return View(dog); 45 } 46 } 47 48 public class Types 49 { 50 public int Tid { get; set; } 51 public string TName { get; set; } 52 } 53 54 }
<!-- edit.cshtml -->
1 @{ 2 Layout = null; 3 } 4 @model MVC知识点.Models.Dog 5 6 <!DOCTYPE html> 7 8 <html> 9 <head> 10 <meta name="viewport" content="width=device-width" /> 11 <title>Edit</title> 12 </head> 13 <body> 14 <div> 15 //1.0 输出form表单的方式1 (html中的弱类型方法) 16 @* <form action="" method="">*@ 17 @{Html.BeginForm("Edit", "C03HtmlHelper", FormMethod.Post);} 18 19 @Html.TextBox("Name", Model.Name) @* <input type="text" name="Name" id="Name" value="小黄" />*@ 20 @Html.Password("Name1", Model.Name)<br /> 21 @Html.CheckBox("Gender", Model.Gender)<br /> 22 @Html.RadioButton("Gender", true, Model.Gender) 23 @Html.RadioButton("Gender", false, !Model.Gender)<br /> 24 @Html.DropDownList("Type", ViewBag.tlist as SelectList)<br /> 25 @Html.ActionLink("跳转", "Index", "C03HtmlHelper") 负责生成a标签 26 27 @{Html.EndForm();} 28 29 <br /> 30 //2.0 输出form表单的方式2 (强类型方法的演示,后面的编辑和新增都使用强类型方法来进行操作) 31 @using (Html.BeginForm("Edit", "C03HtmlHelper", FormMethod.Post)) 32 { 33 @Html.TextBoxFor(c => c.Name, new { style="color:yellow", @class="co" }) <br /> 34 @Html.PasswordFor(c => c.Name)<br /> 35 @Html.CheckBoxFor(c => c.Gender) <br /> 36 @Html.RadioButtonFor(c => c.Gender, true) 37 @Html.RadioButtonFor(c => c.Gender, false) 38 39 @Html.DropDownListFor(c=>c.TypeID,ViewBag.tlist as SelectList) 40 @Html.ValidationMessageFor(c=>c.TypeID) 41 } 42 </div> 43 </body> 44 </html>