forum

Home / DeveloperSection / Forums / refactor Html.LabelFor, EditorFor, ValidationMessageFor into partial view

refactor Html.LabelFor, EditorFor, ValidationMessageFor into partial view

Manoj Bhatt399904-Feb-2013

Hi Everyone!

I want to refactor view form code to avoid copyPaste. But it's not working. Razor don't allow to write

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

@Html.Partial("Item", model => model.EmpName)
Old code, that working:

        <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>
After refactoring not working:

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>
    }
New code:

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

How to make it work?

Thanks in advance


Updated on 04-Feb-2013

Can you answer this question?


Answer

1 Answers

Liked By