1.@Html.ActionLink("列表", "Index") 可以根据路由自动生成相应的超链接
当路由表如下时:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
生成的超链接为:<a href="/UserInfo">列表</a>
当路由表如下时:
routes.MapRoute(
name: "Default",
url: "{controller}-{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
生成的超链接为:<a href="/UserInfo-Index">列表</a>
@Html.ActionLink("列表2", "Index", new { name = "zhangsan", age = "12" }, new { id = "a2" }) 对应生成的超链接为:
<a href="/UserInfo?name=zhangsan&age=12" id="a2">列表2</a>
@Html.ActionLink("列表3", "Index", new { name = "zhangsan", age = "12" }, new { id = "a2",@class="txt" })为a标签添加类要用@class
2 @Html.Raw("<span style='color:red'>asp.net mvc</span>")
原样输出asp.net mvc
3 @Html.Encode("<span style = 'color:red'> asp.net mvc </span >")
编码后输出<span style = 'color:red'> asp.net mvc </span >
4. @Html.DropDownList("city") id,name属性都是city
public ActionResult Register()
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "北京", Value = "1" , Selected=true});
list.Add(new SelectListItem { Text = "深圳", Value = "2" });
list.Add(new SelectListItem { Text = "上海", Value = "3" });
ViewData["city"] = list; //名字和视图下拉框名字相同
return View();
}
@Html.DropDownList("city",null,new { id="selectCity"}) 填充数据,并更改id属性
5
男 @Html.RadioButton("sex","0",true,new { id = "male" })
女 @Html.RadioButton("sex","1")
6 @using (Html.BeginForm())
{
<input type="submit" name="name" value="submit" />
}
默认生成的是form的action还是当前页对应的方法,方法为post
<form action="/UserInfo/Register" method="post">
<input type="submit" name="name" value="submit" />
</form>
@using(Html.BeginForm("Index","UserInfo",FormMethod.Get,new { id = "frm"}))
{
<input type="submit" name="name" value="submit" />
}
生成的是form表单如下:
<form action="/UserInfo" id="frm" method="get">
<input type="submit" name="name" value="submit" />
</form>
7 @Html.TextBox("txtName","请输入名字",new { id="UserName"})