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中 ...
随机推荐
- X210(s5pv210)中断系统
1.SoC对中断的实现机制:异常向量表 (1)异常向量表是CPU中某些特定地址的特定定义.当中断发生的时候,中断要想办法通知CPU去处理中断,怎么做到?这就要靠异常向量表.(2)在CPU设计时,就事先 ...
- shell编程:条件测试与比较(六)
条件测试方法综述 test条件测试的简单语法及测试 范例6-1 测试文件(在test命令中使用-f选项:文件存在且为不同文件则表达式成立) [root@adminset ~]# test -f fil ...
- hive获取日期对应的星期
pmod(datediff(order_date,'2000-01-02'),7)
- [LeetCode] 9. Palindrome Number ☆
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- iBATIS事务处理
一:问题 最近发现了我们自己的项目的事务的处理根本就是行不通的,也因此我自己又去看了下有关事务的处理,算是有了个大致的了解吧,先说说我们最初的配置吧. 二:内容 (1):使用iBatis的事务管理 S ...
- uva 12325 Zombie's Treasure Chest
https://vjudge.net/problem/UVA-12325 题意: 一个箱子,体积为N 两种宝物,体积为S1.S2,价值为V1.V2,数量无限 最多装多少价值的宝物 数据范围:2^32 ...
- 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]
骑士精神 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...
- 【51NOD-0】1018 排序
[算法]排序 #include<cstdio> #include<algorithm> using namespace std; ]; int main() { scanf(& ...
- bzoj 1058 bst
因为是数列的维护,所以我们可以考虑用splay来维护,每次在x插入的时候就在x+1前面插入就行了,然后用bst来维护两问的答案,但是应该会tle.我们来考虑这个问题的性质,首先因为这个数列没有删除操作 ...
- new操作符的内部运行解析
在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassical. 基于上面的例子,我们执行如下代码 ...