Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义html帮助程序中的css id_C#_Css_Asp.net Mvc_Razor - Fatal编程技术网

C# 自定义html帮助程序中的css id

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

我有我的自定义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", 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" })