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

本秂侑毒 提交于 2019-12-06 00:02:26

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!