将 ASP.NET MVC Razor @helper 函数转换为辅助类的方法 [英] Converting ASP.NET MVC Razor @helper function into a method of a helper class

查看:13
本文介绍了将 ASP.NET MVC Razor @helper 函数转换为辅助类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下定义帮助程序的 ASP.NET MVC razor 视图片段

@helper FieldFor(表达式>表达式){<div class="form-group">@Html.LabelFor(表达式,html属性:新 {@class = "control-label col-md-2"})<div class="col-md-10">@Html.EditorFor(表达式,新的{htmlAttributes =新的 {@class = "form-control"}})@Html.ValidationMessageFor(表达式, "",新 {@class = "text-danger"})

}

将其转换为类似于以下类的方法的模式是什么:

public static MvcHtmlString FieldFor(this HtmlHelper helper, FieldFor(Expression> expression)){/* 方法 */}

我发现的所有示例都专注于生成不依赖其他 HTML 帮助程序的标记.

解决方案

签名需要是

public static MvcHtmlString FieldFor(这个 HtmlHelper helper, Expression 表达式)

然后结合使用 TagBuilder 和内置的 html helper 方法来生成 html

使用系统;使用 System.Linq.Expressions;使用 System.Text;使用 System.Web.Mvc;使用 System.Web.Mvc.Html;命名空间 YourAssembly.Html{公共静态类 FieldHelper{public static MvcHtmlString FieldFor<TModel, TValue>(这个HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>>表达式){MvcHtmlString label = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });MvcHtmlString editor = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new {@class = "form-control"}})MvcHtmlString 验证 = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });StringBuilder html = new StringBuilder();html.Append(编辑器);html.Append(验证);TagBuilder innerDiv = new TagBuilder("div");innerDiv.AddCssClass("col-md-10");innerDiv.InnerHtml = html.ToString();html = new StringBuilder();html.Append(标签);html.Append(innerDiv.ToString());TagBuilder outerDiv = new TagBuilder("div");externalDiv.AddCssClass("form-group");externalDiv.InnerHtml = html.ToString();返回 MvcHtmlString.Create(outerDiv.ToString());}}}

然后您可以通过将其添加到 web.config

使其在所有视图中可用

<pages pageBaseType="System.Web.Mvc.WebViewPage"><命名空间><add namespace="System.Web.Mvc"/>....<add namespace="YourAssembly.Html"/></命名空间>

Consider the following ASP.NET MVC razor view snippet which defines a helper

@helper FieldFor<TModel>(Expression<Func<TModel, string>> expression)
{
    <div class="form-group">
        @Html.LabelFor(expression,
                       htmlAttributes:
                           new {@class = "control-label col-md-2"})

        <div class="col-md-10">
            @Html.EditorFor(expression,
                            new
                            {
                                htmlAttributes =
                                new {@class = "form-control"}
                            })
            @Html.ValidationMessageFor(expression, "",
                                       new {@class = "text-danger"})

        </div>
    </div>
}

What is the pattern to convert it to a method of a class similiar to:

public static MvcHtmlString FieldFor(this HtmlHelper helper, FieldFor<TModel>(Expression<Func<TModel, string>> expression))
{
   /* Method */
}

All the examples I have found focus on generating markup which doesn't rely on other HTML helpers.

解决方案

The signature would need to be

public static MvcHtmlString FieldFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)

Then you use a combination of TagBuilder and the inbuilt html helper methods to generate the html

using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace YourAssembly.Html
{
  public static class FieldHelper
  {
    public static MvcHtmlString FieldFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
    {
      MvcHtmlString label = LabelExtensions.LabelFor(helper, expression, new { @class = "control-label col-md-2" });
      MvcHtmlString editor = EditorExtensions.EditorFor(helper, expression, new { htmlAttributes = new {@class = "form-control"}})
       MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class = "text-danger" });

       StringBuilder html = new StringBuilder();
       html.Append(editor);
       html.Append(validation);
       TagBuilder innerDiv = new TagBuilder("div");
       innerDiv.AddCssClass("col-md-10");
       innerDiv.InnerHtml = html.ToString();
       html = new StringBuilder();
       html.Append(label);
       html.Append(innerDiv.ToString());
       TagBuilder outerDiv = new TagBuilder("div");
       outerDiv.AddCssClass("form-group");
       outerDiv.InnerHtml = html.ToString();
       return MvcHtmlString.Create(outerDiv.ToString());
    }
  }
}

Then your can make it available in all you views by adding it to web.config

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      ....
      <add namespace="YourAssembly.Html" />
    </namespaces>

这篇关于将 ASP.NET MVC Razor @helper 函数转换为辅助类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆