在用ASP.NET mvc实际的开发过程中我们会经常用到辅助方法(Helper Method),那么什么是ASP.NET MVC辅助方法Helper Method呢?其实,我们经常用的@Html.Action,@Url.Action些都是ASP.NET MVC内置的一些辅助方法(Helper Method)。今天我来分享一下怎么在ASP.NET MVC中自定义一个辅助方法Helper Method。自定义一个Helper方法一般有两种方式:
1、在视图中内联定义辅助方法Helper Method
2、使用扩展方法下定义外部通用的辅助方法Helper Method
一、在ASP.NET MVC视图中下定义内联辅助方法Helper Method
有时MVC视图有一段代码输出局部的html块,这段代码的逻辑可能在本视图使用多次,而你又不想把它定义成Action和视图来引用。为了减少重复的代码,我们可以定义一个内联辅助方法Helper Method。下面举一个具体的例子:
HomeController.cs:
- using System.Web.Mvc;
- namespace HelperMethods.Controllers {
- public class HomeController : Controller {
- public ActionResult Index() {
- ViewBag.Fruits = new string[] {"Apple", "Orange", "Pear"};
- ViewBag.Cities = new string[] { "New York", "London", "Paris" };
- string message = "This is an HTML element: <input>";
- return View((object)message);
- }
- }
- }
- @model string
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- Here are the fruits:
- @foreach (string str in (string[])ViewBag.Fruits) {
- <b>@str </b>
- }
- </div>
- <div>
- Here are the cities:
- @foreach (string str in (string[])ViewBag.Cities) {
- <b>@str </b>
- }
- </div>
- <div>
- Here is the message:
- <p>@Model</p>
- </div>
- </body>
- </html>
- @model string
- @{
- Layout = null;
- }
- @helper ListArrayItems(string[] items) {
- foreach(string str in items) {
- <b>@str </b>
- }
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- Here are the fruits: @ListArrayItems(ViewBag.Fruits)
- </div>
- <div>
- Here are the cities: @ListArrayItems(ViewBag.Cities)
- </div>
- <div>
- Here is the message:
- <p>@Model</p>
- </div>
- </body>
- </html>
二、使用扩展方法定义一个外部通用的辅助方法Helper Method
上面在视图中定义MVC Helper方法,是很方便,但是也有一个局限就是定义的方法只能用于该方法声明所在的视图。要想在所有的视图中可用就要使用扩展方法定义一个外部通用的辅助方法Helper Method。比如一个分页导航就要在所有的页面都可以使用,你可以看我之前的文章:使用扩展方法生成ASP.NET MVC通用分页页码导航。本例为了简化,我们还是继续使用上面的例子,不过我们现在换成扩展方法,并且我们把之前采用<br>换成<ul>和<li>。定义的扩展方法ListArrayItems代码如下:
- using System.Web.Mvc;
- namespace HelperMethods.Infrastructure {
- public static class CustomHelpers {
- public static MvcHtmlString ListArrayItems(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());
- }
- }
- }
可以看到我们是对HtmlHelper类进行扩展,最外部是一个ul,数组的每一个元素的值是用一个li包起来。接下来我对原来的视图文件/Views/Home/Index.cshtml改成如下代码:
- @model string
- @using HelperMethods.Infrastructure
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- Here are the fruits: @Html.ListArrayItems((string[])ViewBag.Fruits)
- </div>
- <div>
- Here are the cities: @Html.ListArrayItems((string[])ViewBag.Cities)
- </div>
- <div>
- Here is the message:
- <p>@Model</p>
- </div>
- </body>
- </html>
使用使用扩展方法定义一个外部通用的辅助HtmlHelper方法需要注意:
1、开头要引用扩展方法所有的命名空间:@using HelperMethods.Infrastructure
2、调用HtmlHelper方法时,传递参数需要强制转换其相应参数的类型,如:本例是ViewBag的属性C#动态类型,要进行强制类型转换:(string[]), 这一点和视图内联Helper方法有点区别。