重构Html.LabelFor,EditorFor,ValidationMessageFor进入局部视图

时间:2013-02-04 03:53:07

标签: asp.net asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4

我想重构视图表单代码以避免copyPaste。但它不起作用。剃刀不允许写

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

重构后无效:

Item.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)

如何让它发挥作用?

2 个答案:

答案 0 :(得分: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,因为这不是视图。但它确实允许您设置标准控件布局。

答案 1 :(得分:2)

找到了解决方案。 Thanx到@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) });
    }