剃刀HtmlHelpers与小节? [英] Razor HtmlHelpers with sub-sections?

查看:105
本文介绍了剃刀HtmlHelpers与小节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有构建自定义HTML辅助,并付诸小节的方法吗?即:

Is there a way to build the custom Html Helpers and put them into sub-sections? I.e:

@Html.Buttons.Gray
@Html.Buttons.Blue
@Html.Tables.2Columns
@Html.Tables.3Columns

感谢。

推荐答案

助手只是扩展方法。所以,你可以创建一个返回对象助手,让您链方法调用,例如 @ Html.Button(文本),灰色()

Helpers are simply extension methods. So you could create helpers that return object that allow you to chain method calls, e.g. @Html.Button("Text").Grey().

public ButtonHelper
{
   public string Text {get; set;}
   public MvcHtmlString Grey()
   {
      return MvcHtmlString.Create("<button class='grey'>"+ Text +"</button>");
   }
}

public static class Buttons
{
   public static ButtonHelper Button(this HtmlHelper, string text)
   {
      return new ButtonHelper{Text = text};
   }
}

这篇关于剃刀HtmlHelpers与小节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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