新文章 网摘 文章 随笔 日记

MVC视图中扩展helper(泛型绑定Model)

    @functions{
        public HelperResult EditBoxFor<TModel, TKey>(HtmlHelper<TModel> html, Expression<Func<TModel, TKey>> expression, bool disabled = false)
        {
            return EditBox(
                html.LabelFor(expression, htmlAttributes: new { @class = "col-md-3 control-label" }),
                disabled ? html.EditorFor(expression, new { htmlAttributes = new { @class = "form-control", disabled = "" } })
                    : html.EditorFor(expression, new { htmlAttributes = new { @class = "form-control" } }),
                html.ValidationMessageFor(expression, "", new { @class = "text-danger" })
                );
        }
    }
    @helper EditBox(MvcHtmlString label, MvcHtmlString editor, MvcHtmlString validation)
    {
        <div class="form-group">
            @label
            <div class="col-md-9">
                @editor
                @validation
            </div>
        </div>
     }

 

调用: 

@EditBoxFor(Html, model => model.Agent, true)

posted @ 2019-11-28 18:10  岭南春  阅读(199)  评论(0编辑  收藏  举报