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

asp.net-mvc – 如何从对象HtmlAttributes中获取值

发布时间:2020-12-15 18:43:49 所属栏目:asp.Net 来源:网络整理
导读:在asp.net mvc我总是看到内置的html帮助他们总是有对象htmlAttirbutes。 那么我通常会做新的{@id =“test”,@ class =“myClass”}。 如何在我自己的html帮助程序中提取这样的参数? 像我正在使用的“HtmlTextWriterTag”是他们的一种方式,我可以把这整个
在asp.net mvc我总是看到内置的html帮助他们总是有对象htmlAttirbutes。

那么我通常会做新的{@id =“test”,@ class =“myClass”}。

如何在我自己的html帮助程序中提取这样的参数?

像我正在使用的“HtmlTextWriterTag”是他们的一种方式,我可以把这整个对象传递给作家,它弄清楚了什么?

这又如何与大型html帮助者合作?

像我正在制作一个html助手,它使用所有这些标签。

Table
thead
tfooter
tbody
tr
td
a
img

这是否意味着我必须为每个这些标签创建一个html属性?

解决方法

我通常做这样的事情:
public static string Label(this HtmlHelper htmlHelper,string forName,string labelText,object htmlAttributes)
    {
        return Label(htmlHelper,forName,labelText,new RouteValueDictionary(htmlAttributes));
    }

    public static string Label(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes)
    {
        // Get the id
        if (htmlAttributes.ContainsKey("Id"))
        {
            string id = htmlAttributes["Id"] as string;
        }

        TagBuilder tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(htmlAttributes);
        tagBuilder.MergeAttribute("for",true);
        tagBuilder.SetInnerText(labelText);
        return tagBuilder.ToString();
    }

我建议您从codeplex下载ASP.NET MVC源码,并查看内置的html帮助器。

(编辑:李大同)

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

    推荐文章
      热点阅读