As in the topic, is there any way to get HTML tags' attributes in the controller?
The question refers to the following article: https://www.itorian.com/2012/10/html-helper-for-image-htmlimage.html
(I'm sorry I didn't put the link here before, I just thought it's commonly used)
Is any of the following attempts possible?
- First attempt
public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText, string height = "")
{
var builder = new TagBuilder("img");
height != "" ? builder.MergeAttribute("height", height) :
// is there a way to get the size of the <html> tag?
(helper.ViewDataContainer as WebViewPage).Height;
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
- Second attempt
.cshtml file:
@{
Dictionary<string, string> attributes = new Dictionary<string, string>();
attributes.Add("height", // is it possible to get the height here?
Html.Image("...", "...", attributes);
}
c# file:
public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText, Dictionary<string, string> attributes)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", altText);
foreach (var element in attributes)
builder.MergeAttribute(element.Key, element.Value);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}