ASP.NET MVC自定义辅助方法Helper Method

在用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:

 
  1. using System.Web.Mvc;
  2. namespace HelperMethods.Controllers {
  3. public class HomeController : Controller {
  4. public ActionResult Index() {
  5. ViewBag.Fruits = new string[] {"Apple", "Orange", "Pear"};
  6. ViewBag.Cities = new string[] { "New York", "London", "Paris" };
  7. string message = "This is an HTML element: <input>";
  8. return View((object)message);
  9. }
  10. }
  11. }
对应的视图\Views\Home\Index.cshtml:
 
  1. @model string
  2. @{
  3. Layout = null;
  4. }
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta name="viewport" content="width=device-width" />
  9. <title>Index</title>
  10. </head>
  11. <body>
  12. <div>
  13. Here are the fruits:
  14. @foreach (string str in (string[])ViewBag.Fruits) {
  15. <b>@str </b>
  16. }
  17. </div>
  18. <div>
  19. Here are the cities:
  20. @foreach (string str in (string[])ViewBag.Cities) {
  21. <b>@str </b>
  22. }
  23. </div>
  24. <div>
  25. Here is the message:
  26. <p>@Model</p>
  27. </div>
  28. </body>
  29. </html>
可以看到上面输出cities和fruits的逻辑都是一样,只不过数据不一样而已。我们可以定义一个Helper方法来减少重复的代码,将\Views\Home\Index.cshtml内容更改如下:
 
  1. @model string
  2. @{
  3. Layout = null;
  4. }
  5. @helper ListArrayItems(string[] items) {
  6. foreach(string str in items) {
  7. <b>@str </b>
  8. }
  9. }
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta name="viewport" content="width=device-width" />
  14. <title>Index</title>
  15. </head>
  16. <body>
  17. <div>
  18. Here are the fruits: @ListArrayItems(ViewBag.Fruits)
  19. </div>
  20. <div>
  21. Here are the cities: @ListArrayItems(ViewBag.Cities)
  22. </div>
  23. <div>
  24. Here is the message:
  25. <p>@Model</p>
  26. </div>
  27. </body>
  28. </html>
可以从上面看到我们使用@helper在视图中定义了一个Helper方法ListArrayItems,当需要这段逻辑的地方中需要传递参数调用这个ListArrayItems方法就可以了。这样做的好处是减少了冗余的代码和提高了代码了可维护性。

二、使用扩展方法定义一个外部通用的辅助方法Helper Method

上面在视图中定义MVC Helper方法,是很方便,但是也有一个局限就是定义的方法只能用于该方法声明所在的视图。要想在所有的视图中可用就要使用扩展方法定义一个外部通用的辅助方法Helper Method。比如一个分页导航就要在所有的页面都可以使用,你可以看我之前的文章:使用扩展方法生成ASP.NET MVC通用分页页码导航。本例为了简化,我们还是继续使用上面的例子,不过我们现在换成扩展方法,并且我们把之前采用<br>换成<ul>和<li>。定义的扩展方法ListArrayItems代码如下:

 
  1. using System.Web.Mvc;
  2. namespace HelperMethods.Infrastructure {
  3. public static class CustomHelpers {
  4. public static MvcHtmlString ListArrayItems(this HtmlHelper html, string[] list) {
  5. TagBuilder tag = new TagBuilder("ul");
  6. foreach(string str in list) {
  7. TagBuilder itemTag = new TagBuilder("li");
  8. itemTag.SetInnerText(str);
  9. tag.InnerHtml += itemTag.ToString();
  10. }
  11. return new MvcHtmlString(tag.ToString());
  12. }
  13. }
  14. }

可以看到我们是对HtmlHelper类进行扩展,最外部是一个ul,数组的每一个元素的值是用一个li包起来。接下来我对原来的视图文件/Views/Home/Index.cshtml改成如下代码:

 
  1. @model string
  2. @using HelperMethods.Infrastructure
  3. @{
  4. Layout = null;
  5. }
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <meta name="viewport" content="width=device-width" />
  10. <title>Index</title>
  11. </head>
  12. <body>
  13. <div>
  14. Here are the fruits: @Html.ListArrayItems((string[])ViewBag.Fruits)
  15. </div>
  16. <div>
  17. Here are the cities: @Html.ListArrayItems((string[])ViewBag.Cities)
  18. </div>
  19. <div>
  20. Here is the message:
  21. <p>@Model</p>
  22. </div>
  23. </body>
  24. </html>

使用使用扩展方法定义一个外部通用的辅助HtmlHelper方法需要注意:

1、开头要引用扩展方法所有的命名空间:@using HelperMethods.Infrastructure

2、调用HtmlHelper方法时,传递参数需要强制转换其相应参数的类型,如:本例是ViewBag的属性C#动态类型,要进行强制类型转换:(string[]), 这一点和视图内联Helper方法有点区别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值