加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 什么是HtmlHelper渲染htmlAttributes对象的方法

发布时间:2020-12-16 09:12:41 所属栏目:asp.Net 来源:网络整理
导读:我正在构建一个编辑器模板,它将接受htmlAttributes对象. 示例很简单. 在主视图中我有类似的东西 @Html.EditorFor(Function(x) x.SomeProperty,New With {.htmlAttributes = New With {.class = "form-control"}}). 在编辑模板中,我需要编写class =“form-con
我正在构建一个编辑器模板,它将接受htmlAttributes对象.

示例很简单.

在主视图中我有类似的东西

@Html.EditorFor(Function(x) x.SomeProperty,New With {.htmlAttributes = New With {.class = "form-control"}}).

在编辑模板中,我需要编写class =“form-control”属性.

如果在模板中我使用类似@Html.TextBoxFor(Function(x)x,ViewData(“htmlAttributes”))的东西,那么一切都还可以.但是如果我手动构建一个标签,我就无法输出htmlAttributes,即我构建< input type =“text”{htmlAttributes应该在这里} />

是否有任何公共方法可以正确地在标记内呈现HTML属性?

解决方法

我不确定你到底在找什么.传统上,编辑器模板只会传递htmlAttributes.例如:

视图

@Html.EditorFor(m => m.FooString,new { htmlAttributes = new { @class = "foo" } })

String.cshtml

@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue.ToString(),ViewData["htmlAttributes"])

如果您正在询问如何执行类似设置默认值的操作,然后可以通过传递htmlAttributes来覆盖或添加默认值.那么,你在那里几乎就是你自己. MVC中没有任何东西可以帮助你(至少完全没有).但是,我确实编写了自己的HtmlHelper扩展来处理这个问题.我实际上写了一个blog post来解释它的作用以及如何使用它.我建议你检查一下,但我会在这里发布代码以保证完整性.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

public static partial class HtmlHelperExtensions
{
    public static IDictionary<string,object> MergeHtmlAttributes(this HtmlHelper helper,object htmlAttributesObject,object defaultHtmlAttributesObject)
    {
        var concatKeys = new string[] { "class" };

        var htmlAttributesDict = htmlAttributesObject as IDictionary<string,object>;
        var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary<string,object>;

        RouteValueDictionary htmlAttributes = (htmlAttributesDict != null)
            ? new RouteValueDictionary(htmlAttributesDict)
            : HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesObject);
        RouteValueDictionary defaultHtmlAttributes = (defaultHtmlAttributesDict != null)
            ? new RouteValueDictionary(defaultHtmlAttributesDict)
            : HtmlHelper.AnonymousObjectToHtmlAttributes(defaultHtmlAttributesObject);

        foreach (var item in htmlAttributes)
        {
            if (concatKeys.Contains(item.Key))
            {
                defaultHtmlAttributes[item.Key] = (defaultHtmlAttributes[item.Key] != null)
                    ? string.Format("{0} {1}",defaultHtmlAttributes[item.Key],item.Value)
                    : item.Value;
            }
            else
            {
                defaultHtmlAttributes[item.Key] = item.Value;
            }
        }

        return defaultHtmlAttributes;
    }
}

然后,在您的编辑器模板中(此示例中为Date.cshtml):

@{
    var defaultHtmlAttributesObject = new { type = "date",@class = "form-control" };
    var htmlAttributesObject = ViewData["htmlAttributes"] ?? new { };
    var htmlAttributes = Html.MergeHtmlAttributes(htmlAttributesObject,defaultHtmlAttributesObject);
}
@Html.TextBox("",htmlAttributes)

UPDATE

I cannot use @Html.TextBox,but need to write manually <input type="text" {htmlAttributes should be here} />

为什么?帮助器版本没有任何你无法做到的事情.但是,如果你坚持走这条路,那么你将很难用Razor做这件事.假设您实际上可以在没有Razor语法错误的情况下编写它,那么代码将变得虚拟不可读.我建议在纯C#中使用TagBuilder和/或StringBuilder来构造一个字符串,然后确保返回/设置一个MvcHtmlString类型的变量:

var output = new MvcHtmlString(builder.ToString());

但是,如果你走得那么远,它会否定使用编辑器模板的目的,除非你试图覆盖其中一个默认的编辑器模板.无论如何,我建议您只创建自己的HtmlHelper扩展,然后直接在视图中使用它或在编辑器模板中使用它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读