模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式

Max*_*mov 4 asp.net asp.net-mvc razor asp.net-mvc-3 asp.net-mvc-4

我的模特:

public class EmployeeModel
{
    [Required]
    [StringLength(50)]
    [Display(Name = "Employee Name")]
    public string EmpName { get; set; }

    [Required]
    [StringLength(150)]
    [Display(Name = "Email Id")]
    public string Email { get; set; }

    [Required]
    [Range(18, 150)]
    public int Age { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

在我看来:

 @Html.MyEditFor(model=>model.EmpName)
 @Html.MyEditFor(model=>model.Email)
 @Html.MyEditFor(model=>model.Age)
Run Code Online (Sandbox Code Playgroud)

我的自定义助手:

public static MvcHtmlString MyEditFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, object>> expression)
    {
        var partial = html.Partial("Item", new LabelEditorValidation() { Label = html.LabelFor(expression), Editor = html.EditorFor(expression), Validation = html.ValidationMessageFor(expression) }).ToString();
        return MvcHtmlString.Create(partial);
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

LabelEditorValidation - Item.cshtml的模型:

      public class LabelEditorValidation
{
    public MvcHtmlString Validation { get; set; }
    public MvcHtmlString Label { get; set; }
    public MvcHtmlString Editor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有例外

模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式

在线:

    var partial = html.Partial("Item", new LabelEditorValidation() { Label = html.LabelFor(expression), Editor = html.EditorFor(expression), Validation = html.ValidationMessageFor(expression) }).ToString();
Run Code Online (Sandbox Code Playgroud)

唯一的例外occures时 @Html.MyEditFor被调用model.Age.

 @Html.MyEditFor(model=>model.Age) 
Run Code Online (Sandbox Code Playgroud)

但它不是occures时@Html.MyEditFor被调用model.EmpNamemodel.Email.这是因为model.EmpNamemodel.Email都是字符串,但model.Ageint

Vah*_*idN 10

对于Google搜索用户,请不要调用任何Html.XyzFor类似的方法

@Html.CheckBoxFor(model => model.Property***.MyMethod()***)
Run Code Online (Sandbox Code Playgroud)

view models相反,请将其应用于MyMethod给定属性.

  • 我来自Bing :) (5认同)

Dar*_*rov 7

你可以让你的助手更通用,摆脱object争论:

public static MvcHtmlString MyEditFor<TModel, TProperty>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TProperty>> expression
)
{
    var partial = html.Partial(
        "Item", 
        new LabelEditorValidation 
        { 
            Label = html.LabelFor(expression), 
            Editor = html.EditorFor(expression), 
            Validation = html.ValidationMessageFor(expression) 
        }
    ).ToString();
    return MvcHtmlString.Create(partial);
}
Run Code Online (Sandbox Code Playgroud)

现在你的表情不会破坏,因为不会有不必要的拳击.