Home > DeveloperSection > Forums > refactor Html.LabelFor, EditorFor, ValidationMessageFor into partial view
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    February-04-2013 12:43 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 2315  View(s)
Rate this:

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



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    February-04-2013 5:46 AM

Hi Manoj Bhatt!

You can try this way

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

Partial view not support lambda express, so you try this way

@Html.Partial("Item", Model.EmpName)


Don't want to miss updates? Please click the below button!

Follow MindStick