将 MVC Razor @helper函数 ASP.NET 转换为帮助程序类的方法

本文关键字:帮助程序 方法 转换 NET Razor MVC @helper 函数 ASP | 更新日期: 2023-09-27 18:30:30

请考虑以下定义帮助程序的 ASP.NET MVC 剃刀视图代码段

@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>
}

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

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

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

将 MVC Razor @helper函数 ASP.NET 转换为帮助程序类的方法

签名需要

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

然后,使用 TagBuilder 和内置的 html 帮助程序方法的组合来生成 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());
    }
  }
}

然后,您可以通过将其添加到web.config来使其在所有视图中可用

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

这应该很容易。你只需要把它写成HtmlHelper的扩展方法,并使用TagBuilder来构建你的html:

namespace Sample.Extensions
{
    using System;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    public static class TestExtensions
    {
        public static MvcHtmlString FieldFor<TModel>(
                                         this HtmlHelper<TModel> helper,
                                         Expression<Func<TModel, string>> expression)
        {
            var mainDiv = new TagBuilder("div");
            mainDiv.AddCssClass("form-group");
            mainDiv.InnerHtml += helper.LabelFor(expression);
            var colDiv = new TagBuilder("div");
            colDiv.AddCssClass("col-md-10");
            colDiv.InnerHtml += helper.EditorFor(expression, 
                                                 new
                                                 {
                                                    htmlAttributes =
                                                        new {@class = "form-control"}
                                                 })
            colDiv.InnerHtml += helper.ValidationMessageFor(
                                        expression, 
                                        "", 
                                        new {@class = "text-danger"});
            mainDiv.InnerHtml += colDiv;
            return new MvcHtmlString(mainDiv.ToString(TagRenderMode.Normal));
        }
    }
}

查看中的用法:

@using Sample.Extensions
@model ...
...
@Html.FieldFor(m => m.MyField)