C# 自定义html帮助程序中的css id
我有我的自定义html助手,想在那里添加css id标记。 这是我的自定义html帮助程序:C# 自定义html帮助程序中的css id,c#,css,asp.net-mvc,razor,C#,Css,Asp.net Mvc,Razor,我有我的自定义html助手,想在那里添加css id标记。 这是我的自定义html帮助程序: public static class CustomHtmlHelpers { public static IHtmlString Image(this HtmlHelper helper, string src) { TagBuilder tb = new TagBuilder("img"); tb.Attributes.Add("src", Virt
public static class CustomHtmlHelpers
{
public static IHtmlString Image(this HtmlHelper helper, string src)
{
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
}
以下是我在razor视图中的代码:
<div class="display-label">
@Html.DisplayNameFor(model => model.Photo)
</div>
<div class="display-field">
@Html.Image(@Model.Photo)
</div>
但是它不起作用您可以尝试将您的HTML助手更改为:
public static IHtmlString Image(this HtmlHelper helper, object htmlAttributes)
并添加代码:
tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
链接:
能否尝试将HTML助手更改为:
public static IHtmlString Image(this HtmlHelper helper, object htmlAttributes)
并添加代码:
tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
链接:
和html
和html
@Html.Image(@Model.Photo, new { @id = "myId" })