自定义HTML佣工ASP.NET MVC 2 [英] Custom HTML Helpers in ASP.NET MVC 2

查看:108
本文介绍了自定义HTML佣工ASP.NET MVC 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个分页帮手。它需要的唯一参数是当前页,并PAGECOUNT routename。
但是,我不知道是否可以使用我的HTML辅助的定义内的另一个HTML辅助的返回值。我特别是指以Html.RouteLink。我该如何去在类定义做这样的事情。

I want to create a pagination helper. The only parameters that it needs are currentpage, pagecount and routename. However, I do not know if it is possible to use the return value of another html helper inside the definition of my html helper. I am referring specifically to Html.RouteLink. How can I go about doing something like this in the class definition

using System;
using System.Web.Mvc;

namespace MvcApplication1.Helpers
{
     public static class LabelExtensions
     {
          public static string Label(this HtmlHelper helper, string routeName, int currentpage, int totalPages)
          {
               string html = "";
               //Stuff I add to html
               //I'd like to generate a similar result as the helper bellow. 
               //This code does not work, it's just an example of what I'm trying to accomplish
               html .= Html.RouteLink( pageNo, routeName, new { page = pageNo - 1 } );
               //Other stuff I do the html
               return html;

          }
     }
}

感谢您。

推荐答案

一般情况下,是你可以使用自定义功能中的其他HTML辅助函数的结果。唯一的例外是任何直接写入响应流,而不是返回一个字符串值。

Generally, yes you can use the results of other Html Helper functions within your custom functions. The exception would be any that write directly to the response stream rather than returning a string value.

我已经做了这样的事情几次我自己,和它的作品就好了......这是我刚才完全是编造的,现在根据我做的事,我没有code的样品方便的现在:

I've done this sort of thing several times myself, and it works just fine...here's a sample I just totally made up right now based on something I did that I don't have the code for handy right now:

public static string RssFeed(this HtmlHelper helper, string url)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(GetRSSMarkup(url));  // This generates the markup for the feed data
    sb.Append(helper.ActionLink("Go Home","Index","Home"));
    return sb.ToString();
}

这篇关于自定义HTML佣工ASP.NET MVC 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆