MVC - 12.HtmlHelper
1.动态生成URL
- @Url.Action("Index3","Stu3")
- @Url.RouteUrl("Default2", new { controller = "Stu3", action = "Index3", id = UrlParameter.Optional })
2.在视图上直接请求其他action
- @Html.Action("actionName")
3.生成超链接
- @Html.ActionLink("我是超链接~", "Party", "Home", new { id = "newId", style = "color:red" })
4.表单Form
- 4.1.@using (Html.BeginForm())
- 4.2.Begin End
5.弱类型和强类型方法
- 5.1.弱类型方法:@Html.TextBox("Title",model.Title);
- 5.2.强类型方法:@Html.TextBoxFor(s=>s.Name)
6.LabelFor & 模型元数据
- @Html.LabelFor(s=>s.Name)
7.Display / Editor 模型元数据
- [DataType(DataType.Password)]
- @Html.EditorFor(s=>s.Name)
1.动态生成URL

RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Stu", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default2",
url: "{action}/{controller}/{id}",//{action}/{controller}位置改编
defaults: new { controller = "Stu", action = "Index", id = UrlParameter.Optional }
);
Html/index.cshtml
直接编写 url 造成格式固定,不灵活<br/>
<a href="/Stu/Index">1.去玩</a><br />
Url.Action 方法 根据 路由规则生成 url 地址<br />
<a href="@Url.Action("Index3","Stu3")">2.去玩Url.Action</a><br />
Url.RouteUrl 方法 根据 路由 name 生成 url 地址,更灵活<br />
<a href="@Url.RouteUrl("Default2", new { controller = "Stu3", action = "Index3", id = UrlParameter.Optional })">
3.去玩 Url.RouteUrl
</a>
2.在视图上直接请求其他action
@Html.Action("actionName")
参考:8.4 MVC – 8.Razor 布局
3.生成超链接
@Html.ActionLink("我是超链接~", "Party", "Home", new { id = "newId", style = "color:red" })
html源代码
<a href="/Html/Party?Length=4" id="newId" style="color:red">我是超链接~</a>
4.表单Form

4.1.强烈推荐第一种用法
@using (Html.BeginForm("HandleForm", "Home", FormMethod.Post, new { id = "form1" }))
{
<input type="text" />
}
4.2.Begin End
@Html.BeginForm("HandleForm", "Home", FormMethod.Post, new { id = "form2" })
<input type="text" />
@{Html.EndForm();}
5.弱类型和强类型方法
5.1.弱类型方法:
指定 name 和 value
@Html.TextBox("Title",model.Title);
<input id="Title" name="Title" type="text" value="aaaa" /></form>
@Html.RadioButton("chkHabit","篮球",true)
<input checked="checked" id="chkHabit" name="chkHabit" type="radio" value="篮球" />
等其他控件方法也一样
5.2.强类型方法:
强类型方法名有"For"后缀

因为在Razor引擎内部,<> 尖括号在这里被识别为html标签
通过lambda表达式指定模型属性(@model)
@model _06MVCAjax_CodeFirst.Models.Student

@Html.TextBoxFor(s=>s.Name)
@Html.DropDownListFor(s => s.Cid, ViewBag.seList as IEnumerable<SelectListItem>)
特殊案例:
单选按钮:性别
@*<input type="radio" id="GenderFF" name="Gender" value="保密" checked="checked" />保密
<input type="radio" id="GenderM" name="Gender" value="男" />男
<input type="radio" id="GenderW" name="Gender" value="女" />女*@
@Html.RadioButtonFor(s => s.Gender, "保密", new { id = "GenderFF" })保密
@Html.RadioButtonFor(s => s.Gender, "男", new { id = "GenderM" })男
@Html.RadioButtonFor(s => s.Gender, "女", new { id = "GenderW" })女
js
//性别
if (jsonObj.Data.Gender=="男") {
$("#GenderFF").removeAttr("Checked");
$("#GenderW").removeAttr("Checked");
$("#GenderM").attr("Checked","Checked");
}else if (jsonObj.Data.Gender=="女") {
$("#GenderFF").removeAttr("Checked");
$("#GenderM").removeAttr("Checked");
$("#GenderW").attr("Checked","Checked");
}else {
$("#GenderM").removeAttr("Checked");
$("#GenderW").removeAttr("Checked");
$("#GenderFF").attr("Checked","Checked");
}
6.LabelFor & 模型元数据
把姓名,班级,性别改为lable标签
@Html.LabelFor(s=>s.Name)
@Html.LabelFor(s => s.Cid)
@Html.LabelFor(s => s.Gender)
效果如下:

<label for="Name">Name</label>
<label for="Cid">Cid</label>
<label for="Gender">Gender</label>
解决方案:
模型类的元数据包括:属性(名称和类型) 与特性包含的值。
为实体类属性设置 DisplayName 特性:
[DisplayName("班级名")]
public Nullable<int> Cid { get; set; }
注意:DisplayName需要命名空间
using System.ComponentModel;
生成的html代码:
<label for="Name">姓名</label>
7.Display / Editor 模型元数据
@Html.Editor / @Html.Display 可以通过读取特性值生成HTML:
[DataType(DataType.Password)]
public string Name { get; set; }
注意:DataType需要命名空间:
using System.ComponentModel.DataAnnotations;
在 新增/修改 页面上显示某个属性的input标签:
@*<input type="text" id="Name" name="Name" />*@
@Html.EditorFor(s=>s.Name)
生成的html代码:
<input type="password" value="" name="Name" id="Name" class="text-box single-line password">
MVC - 12.HtmlHelper的更多相关文章
- MVC中HtmlHelper用法大全参考
MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...
- MVC 自定义Htmlhelper扩展
在MVC中,我们不仅可以使用它原来的方法,我们还可以自定义,这不不仅加大了我们开发的效率,同时使界面更简洁. 具体什么是扩展方法,你可以这样理解,必须是静态且在形参中第一个参数是以this开头,大概先 ...
- 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性
原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...
- MVC之htmlhelper总结
自学习mvc以来,htmlhelper使用的也非常多,下面开始总结一下吧,由于本人比较懒,内容大部分摘自slark.net的笔记, 有人问为什么用这个htmlhelper,我就喜欢用原生的标签,这个按 ...
- asp.net MVC添加HtmlHelper扩展示例和用法
一.先创建一个HtmlHelper的扩展类,代码: using System; using System.Collections.Generic; using System.Linq; using S ...
- ASP.NET MVC 扩展HtmlHelper类方法
1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- mvc 扩展htmlhelper
using System.Web.Mvc; namespace System.Web.Mvc{ public static class HtmlExtend { public ...
- ASP.NET MVC Razor HtmlHelper扩展和自定义控件
先看示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- MVC采用HtmlHelper扩展和Filter封装验证码的功能
最近因为有个项目除了登录还有其他很多地方需要用到验证码的功能,所以想到了采用HtmlHelper和ActionFilter封装一个验证码的功能,以便能够重复调用.封装好以后调用很方便,只需在View中 ...
随机推荐
- adb 进入 recovery adb 进入 bootloader
重启到Recovery界面 adb reboot recovery重启到bootloader界面 adb reboot bootloader adb wait-for-device #等待设备 adb ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- bzoj 1568 [JSOI2008]Blue Mary开公司 超哥线段树
[JSOI2008]Blue Mary开公司 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1808 Solved: 639[Submit][Sta ...
- bzoj1177 [Apio2009]Oil 二维前缀最大值,和
[Apio2009]Oil Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2300 Solved: 932[Submit][Status][Disc ...
- [Luogu 3958] NOIP2017 D2T1 奶酪
题目链接 人生第一篇题解,多多关照吧. 注意事项: 1.多组数据,每次要先初始化. 2.因为涉及到开根,所以记得开double. 整体思路: 建图,判断「起点」与「终点」是否连通. 方法可选择搜索(我 ...
- Elasticsearch 5.6.5 安装教程
下载地址 https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-5 安装环境 centos6.5 , jdk1.8 ...
- [BZOJ1101&BZOJ2301][POI2007]Zap [HAOI2011]Problem b|莫比乌斯反演
对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 我们可以令F[n]=使得n|(x,y)的数对(x,y)个数 这个很容易得到,只需要让x, ...
- 【Luogu】 P3928 SAC E#1 - 一道简单题 Sequence2
[题目]洛谷10月月赛R1 提高组 [算法]递推DP+树状数组 [题解]列出DP递推方程,然后用树状数组维护前后缀和. #include<cstdio> #include<cstri ...
- 【51NOD-0】1011 最大公约数GCD
[算法]欧几里德算法 #include<cstdio> int gcd(int a,int b) {?a:gcd(b,a%b);} int main() { int a,b; scanf( ...
- DotNETCore 学习笔记 MVC视图
Razor Syntax Reference Implicit Razor expressions <p>@DateTime.Now</p> <p>@DateTim ...