2

Html.LabelFor 内で @class を使用する方法を理解できなかったので、SO にあるコードで Html.Label の拡張ヘルパーを更新しました。

私の LabelExtension.cs ファイルには次のクラスがあります。

 public static class LabelExtensions
    {
        public static string Label(this HtmlHelper helper,
                                string labelFor,
                                string value,
                                object htmlAttributes)
        {
            TagBuilder labelBuilder = new TagBuilder("label");
            if (!string.IsNullOrEmpty(labelFor))
            {
                labelBuilder.Attributes.Add("for", labelFor);
            }
            labelBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            labelBuilder.SetInnerText(value);
            return labelBuilder.ToString(TagRenderMode.Normal);
        } 
    }

私はもともとMVC2 .aspxページで次のものを使用していました(これを.cshtmlに変換しています):

<% Html.BeginForm(); %> <%= Html.Label("txt_name_udq", "Your First Name (required):",
                new {
                @class
                = "mylabelstyle" } )%>
                <br />
                <%= Html.TextBox("txt_name_udq", null, new {
                @class
                = "myinputstyle" } )%>
                <br />
                <%= Html.Label("txt_email_udq", "Your E-Mail Address (required):", new {
                @class
                = "mylabelstyle" } )%>
                <br />
                <%= Html.TextBox("txt_email_udq", null, new {
                @class
                = "myinputstyle" })%>
                <br />
                <% Html.EndForm(); %>

これは MVC2 で非常にうまくレンダリングされました (簡潔にするために usings などを省略しました)。ただし、今日 (_layout.cshtml を使用して) .cshtml に変換しているときに、Html.Label がレンダリングされておらず、代わりにソースが出力されていることがわかりました。コードは次のとおりです。

@section QuoteForm
{
    <div class="entryfull">
        <div class="entryfull_box">
            <div class="entryfull_text">
                <h2>
                    Quote Request
                </h2>
                <ul class="ul-check">
                    <li>Free</li>
                    <li>Confidential</li>
                    <li>Anonymous</li>
                    <li>No Obligation</li>
                </ul>
                @using (Html.BeginForm())
                {
                    @Html.Label("txt_name_udq", "Your First Name (required):", new { @class = "mylabelstyle" })
                    <br />
                    @Html.TextBox("txt_name_udq", null, new { @class = "myinputstyle" })
                    <br />
                    @Html.Label("txt_email_udq", "Your E-Mail Address (required):", new { @class = "mylabelstyle" })
                    <br />
                    @Html.TextBox("txt_email_udq", null, new { @class = "myinputstyle" })
                    <br />
                }
            </div>
        </div>
    </div>
}

上記は単純なものであり、最終製品ではありません。私はそれを最初にレンダリングしようとしています。注: 1) Html.BeginForm のさまざまな反復を試みました。と 2) に同封しました。それでも、それは「機能」していません。

テキストボックス (レンダリング) のすぐ上にあるラベル (ブラウザーで出力されたソース) のブラウザーに表示されるものを次に示します。

<label class="mylabelstyle" for="txt_name_udq">Your First Name (required):</label>;

「ソースを表示」すると、次のように表示されます。

&lt;label class=&quot;mylabelstyle&quot; for=&quot;txt_name_udq&quot;&gt;Your First Name (required):&lt;/label&gt;;

これは@sectionと関係がありますか?それとも、MVC2 の拡張機能を使用しようとしているのですか?

ご意見/アドバイスをお寄せいただきありがとうございます。

編集:これは、私の状況で機能したコメントで参照するコードです。助けてくれてありがとう。

public static MvcHtmlString Label(this HtmlHelper htmlHelper, string labelFor, string value,
                                      object htmlAttributes)
    {
        var tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tagBuilder.MergeAttribute("for", labelFor.Replace(".", tagBuilder.IdAttributeDotReplacement), true);
        tagBuilder.SetInnerText(value);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
    }
4

1 に答える 1

4

これは、ヘルパーが文字列を返し、デフォルトでエンコードされているためです。ヘルパーは MvcHtmlString を返し、return ステートメントを次のように変更する必要があります。

 return new MvcHtmlString(labelBuilder.ToString(TagRenderMode.Normal));
于 2011-04-01T16:34:48.067 に答える