当前位置:首页 > Web开发 > 正文

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

2024-03-31 Web开发

@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>
            @label
            <div>
                @editor
                @validation
            </div>
        </div>
     }

调用: 

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

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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/42476.html