daguanjia11的专栏

本篇文章我们来讨论ASP.NET MVC框架中的Helper Method。就是经常用到的类似@Html.TextBox(“userName”)的表达式。本篇主要包含两个内容,1.创建自己定义的Helper Method,2.使用MVC框架内置的Helper Method。 Helper Method可以用来封装少量的HTML代码,用更简单的方式来输出常用的内容,就像@Html.TextBox(“userName”)这样,是最轻量级的前端代码重用。如果要重用的代码比较复杂,则不建议使用Helper Method来封装,而是采用Partial View 和Child Action来实现。

创建自己定义的Helper Method创建页面内的Helper Method

举个例子,我们项目中的HomeController类中有一个名叫HelperMethodTest的Action方法,以及对应的View,下面是Action方法的代码

namespace MvcApplication1.Controllers{public class HomeController : Controller{public ActionResult HelperMethodTest(){ViewBag.Fruits = new string[] { “Apple”, “Orange”, “Pear” };ViewBag.Cities = new string[] { “New York”, “London”, “Paris” };return View();}}}

下面是View的代码

@{ViewBag.Title = “HelperMethodTest”;}<h2>HelperMethodTest</h2><div>Here are the fruits:<ul>@foreach (string str in (string[])ViewBag.Fruits){<li>@str </li>}>Here are the cities:<ul>@foreach (string str in (string[])ViewBag.Cities){<li>@str </li>}</ul></div>

很明显,在View中存在两个具有相同逻辑的foreach语句段,我们可以通过Helper Method来封装一下,下面是修改后的代码

@{ViewBag.Title = “HelperMethodTest2”;}<h2>HelperMethodTest2</h2>@helper DisplayList(string[] items){<ul>@foreach (var item in items){<li>@item</li>}</ul>}<div>Here are the fruits:@DisplayList(ViewBag.Fruits)</div><div>Here are the cities:@DisplayList(ViewBag.Cities)</div>

是不是感觉写代码的逼格瞬间得到了提升?@helper DisplayList(string[] items)定义了一个Helper Method,像一个普通方法一样,,但是没有返回值类型,我们在方法体中直接输出HTML标签就行了。@DisplayList(ViewBag.Cities)用来调用。

创建外部的Helper Method

使用上面这种方式定义的Helper Method,只能在该View页面内调用,如果希望编写一个更加通用的Helper Method,在所有的地方都能够调用的话,也是可以做到的,但是写起来感觉有些许的蹩脚。 首先,我们右键点击Models文件夹,新建一个类,命名为”CustomHelperMethod“,把该类定义为静态的,然后分别引入命名空间System.Web和System.Web.Mvc,下面是完整的代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication1.Models{CustomHelperMethod{public static MvcHtmlString DisplayList(this HtmlHelper html, string[] list){TagBuilder tag = new TagBuilder(“ul”);foreach (string str in list){TagBuilder itemTag = new TagBuilder(“li”);itemTag.SetInnerText(str);tag.InnerHtml += itemTag.ToString();}return new MvcHtmlString(tag.ToString());}}}

定义好了方法以后,在View中使用

@{ViewBag.Title = “HelperMethodTest3”;}@using MvcApplication1.Models<h2>HelperMethodTest3</h2><div>Here are the fruits:@Html.DisplayList((string[])ViewBag.Fruits)</div><div>Here are the cities:@Html.DisplayList((string[])ViewBag.Cities)</div>

注意,@Html.DisplayList方法调用时,参数需要进行强制类型转换,否则编译不过,不知道为什么。

Helper Method中的编码问题

最后一行代码return new MvcHtmlString(tag.ToString());,之所以通过这样的方式返回,是因为MVC框架默认会把发回到页面上的数据自动编码,但如果我们真的想返回代用’<’或’>’的代码时,可以通过返回一个MvcHtmlString对象来实现。下面用例子演示一下,在一个Action方法中

public ActionResult Test(){ViewBag.Message = “<b>ABC</b>”;return View();}

在View页面中

@ViewBag.Message

这样子显示出来的并不是加粗的ABC,而是”< b>ABC< /b>”。如果把ViewBag.Message修改为一个MvcHtmlString对象

public ActionResult Test(){ViewBag.Message = new MvcHtmlString(“<b>ABC</b>”);return View();}

呈现出来的就是加粗的ABC了,这就是我们想要的结果。

使用MVC框架内置的Helper Method使用Helper Method创建Form既有美妙的风景,也会有称不上景只有风的地方。

daguanjia11的专栏

相关文章:

你感兴趣的文章:

标签云: