.NET MVC HtmlHepler

一、HtmlHepler

1.ActionLink()

动态生成 超链接:根据路由规则,生成对应的 html 代码。

//1.注册路由信息

routes.MapRoute(

name: "Default",

url: "{controller}_aa/{action}.html/{id}",

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

);

//2.在试图上创建 超链接

<a href="/Home/Heaven">回家</a><br />

@Html.ActionLink("回家","heaven", "Home")

//3.在浏览器看到的生成结果

<a href="/Home/Heaven">回家</a><br />

<a href="/Home/heaven">回家</a>

2.BeginForm()

//1.在视图中 创建 表单

@using (Html.BeginForm("add", "home", FormMethod.Post, new { id="form1" }))

{

}

//2.生成的html代码

<form action="/home/add.html" id="form1" method="post">        文章标题:

</form>

补充:

//直接 在视图中 @ 调用有返回值的方法,就已经相当于是将返回值 写入 Response了

@Html.Label("ATitle")

//相当于下面代码

@{

Response.Write(Html.Label("ATitle"));

}

3.Lable()等生成 html 标签方法

注:所有的方法 都默认 去 视图的 Model 属性所储存的对象 中 查找匹配属性

//1.为实体类 添加 特性 DisplayName

public partial class BlogArticle

{

[DisplayName("文章标题")]

public string ATitle { get; set; }

}

//2.在Action方法中,为视图 的 Model 设置值

public ActionResult Add()

{

return View(new Models.BlogArticle() { ATitle="哇哈哈哈~~!" });

}

//3.在视图中,通过 html的帮助方法,生成 html 标签,同时指定,要读取的 属性名

@Html.Label("Atitle")

//4.生成对应的 html标签,并自动读取了 对应属性 的 DisplayName 里的文本。

<label for="Atitle">文章标题</label>

4.强类型的Html标签方法

强类型方法,直接 通过 lambda表达式,去试图的 Model属性对象中 查找对应的属性数据

4.1普通强类型方法(通过不同方法生成不同html标签)

//1.视图上调用方法

@Html.TextBoxFor(a=> a.ATitle)

//2.生成的html代码

<input class="text-box single-line" id="ATitle" name="ATitle" type="text" value="哇哈哈哈~~!"/>

4.2超强强类型方法(通过 属性的 DataType特性生成html标签)

//1.在实体类中 为 AContent 属性设置 DataType特性,指定为 多行文本框

public partial class BlogArticle

{

[DataType(System.ComponentModel.DataAnnotations.DataType.MultilineText)]

public string AContent { get; set; }

}

//2.视图上 自动根据model对象里属性保存的 实体类 属性 的 [DataType] 特性里指定的类型生成对应的html标签

@Html.EditorFor(a=> a.ATitle)

//3.生成html代码

<textarea class="text-box multi-line" id="AContent" name="AContent"></textarea>

上一篇:洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流


下一篇:Linux下php安装memcache扩展