Creating Unorderlist using HTML Helpers with custom Attribute
Please refer to know how to use Extend HTML Helpersย ย here
.CS File
namespace CustomeHelpers
{
public static class CreatingUnorderedList
{
public static MvcHtmlString CreatingUnorderedListUsingAttribute(this HtmlHelper htmlHelper, string name, string value, string id)
{
var builder = new TagBuilder(“li”);
builder.Attributes
builder.Attributes[“type”] = “radio”;
builder.Attributes[“name”] = name;
builder.Attributes[“value”] = value;
//creating Custom attribue in the Element
builder.Attributes[“listId”] = name + “-” + id;
if (id == “0”)
builder.SetInnerText(“Start List ” + id);
else if (id == “4”)
builder.SetInnerText(“End List ” + id);
else
builder.SetInnerText(“List ” + id);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
}
}
Note : SetInnerText used for set the text/value for the element
View
<h2>Generating Unordered List using HTML Helpers</h2>
<ul>
@for (var loop_index = 0; loop_index < 5; loop_index++)
{
@Html.CreatingUnorderedListUsingAttribute(“custom_txtbox”, “Custom Helper Method”,loop_index.ToString())
}
</ul>
OUTPUT :
Below image showsย Unordered List with custom attribute listId in the li element
Leave A Comment