Questions tagged [tagbuilder]
38 questions
42
votes
6 answers
Why use TagBuilder instead of StringBuilder?
what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable?
aren't they generating the same thing??

DaveDev
- 41,155
- 72
- 223
- 385
30
votes
3 answers
Nested TagBuilder -as TagBuilderTree-
TagBuilder is a nice implementation for build HTML elements. But -some- HTML elements can have another elements (I called like children). I could not find any class from Mvc classes.
Question; Should I implement few classes (TagBuilderTree, and…

Nuri YILMAZ
- 4,291
- 5
- 37
- 43
6
votes
2 answers
TagBuilder.MergeAttributes does not work
I am creating my own helper in MVC. But the custom attributes are not added in the HTML:
Helper
public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes)
{
var…

ridermansb
- 10,779
- 24
- 115
- 226
6
votes
1 answer
How to load a partialview and store it in a variable to be used in javascript?
I have a prtialview using a model to create a dropdown list.
In my main view I create the partialview html string to use it in javascript functions later.
View:
@model SomeViewModel
@{
var devicesDropDown = @Html.Partial("_DropDown",…

Forough
- 262
- 4
- 20
5
votes
3 answers
Can you control the order in which the TagBuilder class renders attributes?
I know this is kind of obsessive, but is there a way to control the order that the TagBuilder class renders the attributes of an HTML tag when you call ToString()?
i.e. so that
var tb = new TagBuilder("meta"); …

Glenn Slaven
- 33,720
- 26
- 113
- 165
5
votes
2 answers
Html.ActionLink extension
I am trying to extend the Html.ActionLink as I want to add a custom meta data for a shared component (in this case a modal).
My aim is to further extend the LinkExtensions class within .Net MVC which will add a value to the html class attribute and…

Andy Clark
- 3,363
- 7
- 27
- 42
4
votes
2 answers
TagBuilder add attribute without value
The same question from here.
How to add attribute without value
This time, I want to create an input with AngularJS attribute, to use it as a directive.
Here is what I want:
This is what the TagBuilder generated:

Huy Hoang Pham
- 4,107
- 1
- 16
- 28
4
votes
3 answers
What is the difference, if any, between string.Format and TagBuilder in ASP.NET MVC?
I have a Html Helper file for my ASP.NET MVC application. The majority of them simply return a formatted string.
Here is an example of one of my formatted string helpers:
public static string Label(this HtmlHelper helper, string @for, string…

Anders
- 12,088
- 34
- 98
- 146
3
votes
1 answer
ASP.NET MVC - Unit testing HTML that is generated by TagBuilders
I have code that generates html via TagBuilders How could I test if the right HTML is generated? My TagBuilder is not as simple as just one tag, it also contains other tags. So I have a function that returns TagBuilder object with following…

dev.e.loper
- 35,446
- 76
- 161
- 247
3
votes
1 answer
RouteValueDictionary to HtmlAttributes
I know I can add html attributes to my tag by doing something like:
var htmlAttributes = new RouteValueDictionary { { "data-foo", "bar" } };
var tag = new TagBuilder("div");
tag.MergeAttributes(htmlAttributes );
@tag
Output:

Steven Wexler
- 16,589
- 8
- 53
- 80
3
votes
1 answer
Prevent TagBuilder.MergeAttribute from encoding attribute text
I've created a Html helper that encodes email addresses in order to prevent SPAM. This is the same technique used by the MarkdownSharp library when auto-generating email links.
The problem is that TagBuilder.MergeAttribute encodes the attribute text…

Ben Foster
- 34,340
- 40
- 176
- 285
3
votes
2 answers
ASP.NET MVC TagBuilder.SetInnerText() not setting the text correctly
So I am creating a HtmlHelper extension method and I have run into a problem when using TagBuilder.SetInnerText(). The helper outputs an option tag. Here is the source of the helper:
public static string Option(this HtmlHelper helper, string…

Tim Banks
- 7,099
- 5
- 31
- 28
3
votes
1 answer
When to use MergeAttribute over Attributes.Add
When using TagBuilder one can use TagBuilder.Attributes.Add(..) or TagBuilder. MergeAttribute(..) to add attributes to the HTML element under construction:
TagBuilder formBuilder = new TagBuilder("form");
formBuilder.Attributes.Add("method",…

Henrik Stenbæk
- 3,982
- 5
- 31
- 33
2
votes
1 answer
Disable HTML encoding of attribute values in TagBuilder
I'm using MVC and I've created a HtmlHelper extension function ImageLink. This function uses two TagBuilder objects to build an HTML image link (like
).
It works fine, but now I have a page on which the action for the image button…

Jasper
- 1,697
- 2
- 23
- 48
2
votes
2 answers
Asp.NET MVC 6, TagHelper "asp-for" to Model itself
Lets say I have this partial View:
@model DateTime?
What goes to ??? to bind to the Model its self?

Skorunka František
- 5,102
- 7
- 44
- 69