1

copyPaste を回避するために、ビュー フォーム コードをリファクタリングしたいと考えています。しかし、それは機能していません。Razorは書き込みを許可しません

@model System.Linq.Expressions.Expression<Func<TModel, TValue>> expression

@Html.Partial("Item", model => model.EmpName)

古いコード、その作業:

        <tr>
            <td class="editor-label" style="border: 0;">
                @Html.LabelFor(model=>model.EmpName)
            </td>
            <td class="editor-field" style="border: 0">
                @Html.EditorFor(model=>model.EmpName)
                @Html.ValidationMessageFor(model=>model.EmpName)
            </td>
        </tr>
        <tr>
            <td class="editor-label" style="border: 0;">
                @Html.LabelFor(model=>model.Email)
            </td>
            <td class="editor-field" style="border: 0;">
                @Html.EditorFor(model=>model.Email)
                @Html.ValidationMessageFor(model=>model.Email)
            </td>
        </tr>

リファクタリングが機能しない後:

アイテム.cshtml:

   @model System.Linq.Expressions.Expression<Func<TModel, TValue>> expression
        <tr>
            <td class="editor-label" style="border: 0;">
                @Html.LabelFor(expression)
            </td>
            <td class="editor-field" style="border: 0">
                @Html.EditorFor(expression)
                @Html.ValidationMessageFor(expression)
            </td>
        </tr>
    }

新しいコード:

 @Html.Partial("Item", model => model.EmpName)
 @Html.Partial("Item", model => model.Email)

それを機能させる方法は?

4

2 に答える 2

2

解決策が見つかりました。@Paulに感謝します。

Item.cshtml:

 @model MyClientCustomValidation.Models.LabelEditorValidation 
        <tr>
            <td class="editor-label" style="border: 0;">
                @Model.Label
            </td>
            <td class="editor-field" style="border: 0">
                @Model.Editor
                @Model.Validation
            </td>
        </tr>

モデルがあります:

public class LabelEditorValidation
{
    public MvcHtmlString Label { get; set; }
    public MvcHtmlString Editor { get; set; }
    public MvcHtmlString Validation { get; set; }     
}

そして内部の形:

@Html.MyEditFor(model=>model.EmpName)
@Html.MyEditFor(model=>model.Email)

MyEditorForはどこにありますか

public static MvcHtmlString MyEditFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
    {
        return html.Partial("Item", new LabelEditorValidation() { Label = html.LabelFor(expression), Editor = html.EditorFor(expression), Validation = html.ValidationMessageFor(expression) });
    }
于 2013-02-04T10:18:45.357 に答える
2

次のような独自のHtmlHelper拡張メソッドを作成できます。

public static MvcHtmlString MyEditFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    var group = new TagBuilder("div");
    group.InnerHtml = html.LabelFor(expression).ToString();
    //more formatting and controls here
    return MvcHtmlString.Create(group.ToString());
}

これにより、次のように書くことができます。

@Html.MyEditFor(m => m.Name)

このアプローチの欠点は、これがビューではないため、HTMLをインライン化できないことです。ただし、標準のコントロールレイアウトを設定することはできます。

于 2013-02-04T05:57:26.223 に答える