<1>YYController 控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Collections; namespace MvcFirst.Controllers { public class YYController : Controller { // // GET: /YY/ public ActionResult Index() { return View(); } public ActionResult HttpHelper() { //给下拉框赋值的第一种写法 IList<SelectListItem> list = new List<SelectListItem>(); SelectListItem item1 = new SelectListItem() { Selected = false, Text = "北京", Value = "1" }; SelectListItem item2 = new SelectListItem() { Selected = false, Text = "上海", Value = "2" }; SelectListItem item3 = new SelectListItem() { Selected = false, Text = "广州", Value = "3" }; list.Add(item1); list.Add(item2); list.Add(item3); ViewData["City"] = list; //给下拉框赋值的第二种写法 ViewData["Province"] = new List<SelectListItem>(){ new SelectListItem(){Selected=true,Text="湖南",Value="hn"}, new SelectListItem(){Selected=false,Text="广东",Value="gd"}, new SelectListItem(){Selected=false,Text="江苏",Value="js"}, }; ViewData["UserName"] = "你好,中国"; return View(); } } }
<2>视图
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>HttpHelper</title> </head> <body> <div> <%-- ------------------------------------------------------------DropDownList下拉列表-----------------------%> <%--这个DropDownList的name属性值为“City”,那么它就会自动去控制器里面去找Key为City的ViewData,然后将ViewData["City"]的数据往Html.DropDownList里面装配【即将ViewData["City"]的数据绑定到Html.DropDownList上来】--%> <%:Html.DropDownList("City") %> <%:Html.DropDownList("Province")%> <br /> <%--因为ViewData["City"]是一个list所以不能用这种字典来取值--%> <%--<%:ViewData["City"] %>--%> <%-- ---------------------------------------------------------TextBox 单行文本框----------------------------%> <%--这个TextBox的name属性值为“UserName”,那么它可以自动去控制器的里面去取key为UserName的ViewData,让后给ViewData["UserName"]的值赋给Html.TextBox()的value属性--%> <%:Html.TextBox("UserName") %><br /> <%-- ---------------------------------------------------------TextArea 多行文本框----------------------------%> <%--创建一个5行6列的多行文本框,文本框的默认值为"",并给它添加一个id属性并赋值为txts--%> <%:Html.TextArea("txt1","",5,6,new {id="txts"}) %><br /> <%-- ------------------------------------------------------RadiodButton单选框----------------------%> <%--Html.RadioButton的name属性为radio1,这三个Html.RadioButton的name属性一定要一致,才可以互斥--%> 男<%:Html.RadioButton("radio1", "男", true, new {id="la" })%> 女<%:Html.RadioButton("radio1","女",false) %> 保密<%:Html.RadioButton("radio1","保密",false)%><br /> <%-- ------------------------------------------------------CheckBox复选框----------------------%> 跑步<%:Html.CheckBox("CheckBox1",true) %> <%--给Html.CheckBox这个复选框的name值设为CheckBox2,设为选中,并给它添加两个属性,一个属性是id,并赋值"langqiu",另外一个属性是lable,并赋值为”篮球“--%> 篮球<%:Html.CheckBox("CheckBox2", true, new { id="langqiu", lable = "篮球"})%> 骑马<%:Html.CheckBox("CheckBox3",false) %><br /> </div> </body> </html>
然后打开该页面的源文件看看,它们都解析成什么样了?
<!DOCTYPE html> <html> <head><meta name="viewport" content="width=device-width" /><title> HttpHelper </title></head> <body> <div> <select id="City" name="City"><option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> </select> <select id="Province" name="Province"><option selected="selected" value="hn">湖南</option> <option value="gd">广东</option> <option value="js">江苏</option> </select> <br /> <input id="UserName" name="UserName" type="text" value="你好,中国" /><br /> <textarea cols="6" id="txts" name="txt1" rows="5"> </textarea><br /> 男<input checked="checked" id="la" name="radio1" type="radio" value="男" /> 女<input id="radio1" name="radio1" type="radio" value="女" /> 保密<input id="radio1" name="radio1" type="radio" value="保密" /><br /> 跑步<input checked="checked" id="CheckBox1" name="CheckBox1" type="checkbox" value="true" /><input name="CheckBox1" type="hidden" value="false" /> 篮球<input checked="checked" id="langqiu" lable="篮球" name="CheckBox2" type="checkbox" value="true" /><input name="CheckBox2" type="hidden" value="false" /> 骑马<input id="CheckBox3" name="CheckBox3" type="checkbox" value="true" /><input name="CheckBox3" type="hidden" value="false" /><br /> </div> </body> </html>
时间: 2024-10-17 11:46:29