与包括剃刀声明MvcHtmlString [英] MvcHtmlString with Razor statements included

查看:143
本文介绍了与包括剃刀声明MvcHtmlString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我这个重复code座往往...

 < D​​IV CLASS =表单组>
            < D​​IV CLASS =COL-SM-3控制标签>
                @ Html.LabelFor(M = GT; m.Name)
            < / DIV>
            < D​​IV CLASS =COL-SM-9>
                @ Html.TextBoxFor(M = GT; m.Name,空,新的{@class =表格控})
                @ Html.ValidationMessageFor(M = GT; m.Name)
            < / DIV>
        < / DIV>

我一直在尝试创建一个自定义的MvcHtmlString扩展,但它假定@ Html.LabelFor命令是文本,这是显示的内容。

编辑:谢谢乔和斯蒂芬!这就是我失踪了。

下面是我的code座最后的答案

 静态MvcHtmlString BaseFieldFor<的TModel>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,串>>前pression,MvcHtmlString的innerHTML,串风格= NULL){        VAR labelDiv =新TagBuilder(分区);        labelDiv.AddCssClass(COL-SM-3控制标签);
        labelDiv.InnerHtml + = helper.LabelFor(如pression,新{
            htmlAttributes = {新@class =表格控}
        });        VAR textDiv =新TagBuilder(分区);        textDiv.AddCssClass(COL-MD-9);
        textDiv.InnerHtml + = innerHTML的;
        textDiv.InnerHtml + = helper.ValidationMessageFor(如pression);        VAR groupDiv =新TagBuilder(分区);        groupDiv.AddCssClass(表组);
        groupDiv.InnerHtml + = labelDiv;
        groupDiv.InnerHtml + = textDiv;        返回新MvcHtmlString(groupDiv.ToString(TagRenderMode.Normal));
    }

和使用

 公共静态MvcHtmlString FieldFor<的TModel>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,串>>前pression,串风格=空值) {        VAR的innerHTML = helper.TextBoxFor(如pression,空,新的{@class =表格控,风格});        返回BaseFieldFor(辅助,前pression,innerHTML的,风格);
    }    公共静态MvcHtmlString DropDownListFor<的TModel>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,串>>前pression,IEnumerable的< SelectListItem>列表中,字符串风格= NULL){        VAR的innerHTML = helper.DropDownListFor(如pression,新的SelectList(列表中,值,文本),新{@class =表格控,风格});        返回BaseFieldFor(辅助,前pression,innerHTML的,风格);
    }

现在我可以用它简单!

 < D​​IV CLASS =面板身体形态水平的>
        @ Html.FieldFor(M = GT; m.Name)
        @ Html.FieldFor(M = GT; m.Address1)
        @ Html.FieldFor(M = GT; m.Address2)
        @ Html.FieldFor(M = GT; m.City)
        @ Html.DropDownListFor(M = GT; m.State,AllowableValues​​.StateList,最大宽度:200像素;)
        @ Html.FieldFor(M = GT; m.Postal code,最大宽度:150像素;)
        @ Html.FieldFor(M = GT; m.Phone,最大宽度:150像素;)
    < / DIV>


解决方案

您帮助需要使用内置的辅助方法生成的HTML。例如:

  MvcHtmlString标签= LabelExtensions.LabelFor(辅助,前pression});

<一个href=\"http://stackoverflow.com/questions/26955073/converting-asp-net-mvc-razor-helper-function-into-a-method-of-a-helper-class/26955246#26955246\">Refer这个例子的用于创建类似的输出,包括HTML标签,文本框和验证消息的帮手。

I find myself repeating this code block too often...

        <div class="form-group">
            <div class="col-sm-3 control-label">
                @Html.LabelFor(m => m.Name)
            </div>
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.Name, null, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Name)
            </div>
        </div>

I have been attempting to create a MvcHtmlString custom extension, but it assumes that the @Html.LabelFor commands are text and that is what is displayed.

EDIT: Thanks Joe and Stephen!! That's what I was missing.

Here's the final answer for my code block

    static MvcHtmlString BaseFieldFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, string>> expression, MvcHtmlString innerHtml, string style = null) {

        var labelDiv = new TagBuilder("div");

        labelDiv.AddCssClass("col-sm-3 control-label");
        labelDiv.InnerHtml += helper.LabelFor(expression, new {
            htmlAttributes = new { @class = "form-control" }
        });

        var textDiv = new TagBuilder("div");

        textDiv.AddCssClass("col-md-9");
        textDiv.InnerHtml += innerHtml;
        textDiv.InnerHtml += helper.ValidationMessageFor(expression);

        var groupDiv = new TagBuilder("div");

        groupDiv.AddCssClass("form-group");
        groupDiv.InnerHtml += labelDiv;
        groupDiv.InnerHtml += textDiv;

        return new MvcHtmlString(groupDiv.ToString(TagRenderMode.Normal));
    }

and for usage

    public static MvcHtmlString FieldFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, string>> expression, string style = null) {

        var innerHtml = helper.TextBoxFor(expression, null, new { @class = "form-control", style });

        return BaseFieldFor(helper, expression, innerHtml, style);
    }

    public static MvcHtmlString DropDownListFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, string>> expression, IEnumerable<SelectListItem> list, string style = null){

        var innerHtml = helper.DropDownListFor(expression, new SelectList(list, "Value", "Text"), new { @class = "form-control", style });

        return BaseFieldFor(helper, expression, innerHtml, style);
    }

And now I can use it simply!

    <div class="panel-body form-horizontal">
        @Html.FieldFor(m => m.Name)
        @Html.FieldFor(m => m.Address1)
        @Html.FieldFor(m => m.Address2)
        @Html.FieldFor(m => m.City)
        @Html.DropDownListFor(m => m.State, AllowableValues.StateList, "max-width: 200px;")
        @Html.FieldFor(m => m.PostalCode, "max-width: 150px;")
        @Html.FieldFor(m => m.Phone, "max-width: 150px;")
    </div>

解决方案

Your helper needs to use the inbuilt helper methods for generating your html. For example

MvcHtmlString label = LabelExtensions.LabelFor(helper, expression});

Refer this example for creating a helper that outputs similar html including the label, textbox and validation message.

这篇关于与包括剃刀声明MvcHtmlString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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