模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引前pressions使用 [英] Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions

查看:240
本文介绍了模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引前pressions使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:

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; }

}

在我看来:

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

我的定制帮手:

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);
    }

Item.cshtml - 局部视图:

Item.cshtml - partial view:

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

LabelEditorValidation - 模型Item.cshtml:

LabelEditorValidation - model for Item.cshtml:

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

我有例外

模板只能与现场访问,访问属性可以使用,
  一维数组的索引,或单参数自定义索引
  前pressions

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions

这行:

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

例外occures当 @ Html.MyEditFor 被调用 model.Age

 @Html.MyEditFor(model=>model.Age) 

但它不是occures当 @ Html.MyEditFor 被调用 model.EmpName model.Email
这是因为 model.EmpName model.Email 都是字符串,但 model.Age INT

But it not occures when @Html.MyEditFor is called for model.EmpName and model.Email. That's because model.EmpName and model.Email are strings but model.Age is int

推荐答案

您可以使你的帮手多了几分通用和摆脱对象的说法:

You could make your helper a little more generic and get rid of the object argument:

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);
}

现在你的前pression不会打破因为不会有不必要的拳。

Now your expression won't break as there won't be unnecessary boxing.

这篇关于模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引前pressions使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆