如何获得价值out对象HtmlAttributes的 [英] How to get values out of object HtmlAttributes
问题描述
在asp.net mvc的我总是看到内置的HTML辅助他们总是有对象htmlAttirbutes。
In asp.net mvc I always see the built in html helpers they always have object htmlAttirbutes.
然后我通常做新{@id =测试,@类=MyClass的}。
Then I usually do new {@id = "test", @class="myClass"}.
我如何提取这样的参数在我自己的HTML佣工?
How do I extract a parameter like this in my own html helpers?
就像我使用的HtmlTextWriterTag是他们的一个方法可以让我这整个对象传递给作家和它想通了还是什么?
Like I am using "HtmlTextWriterTag" is their a way I can pass this whole object to the writer and it figured it out or what?
另外如何与大HTML辅助这项工作?
Also how does this work with big html helpers?
就像我想提出一个HTML辅助,它使用所有这些标签。
Like I am making a html helper and it uses all these tags.
Table
thead
tfooter
tbody
tr
td
a
img
这是否意味着我必须做出一个HTML属性为每个标签?
Does that mean I have to make a html Attribute for each of these tags?
推荐答案
我平时做这样的事情:
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, string forName, string labelText,
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", forName, true);
tagBuilder.SetInnerText(labelText);
return tagBuilder.ToString();
}
我建议您下载从codePLEX的ASP.NET MVC源看一看内置的HTML辅助。
I suggest you to download the ASP.NET MVC source from the codeplex and take a look at the built in html helpers.
这篇关于如何获得价值out对象HtmlAttributes的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!