簡體   English   中英

將ASP.NET MVC Razor @helper函數轉換為輔助類的方法

[英]Converting ASP.NET MVC Razor @helper function into a method of a helper class

請考慮以下ASP.NET MVC razor視圖片段,它定義了一個幫助器

@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幫助程序的標記。

簽名需要

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM