0

The following C# codes creates image tag which fetches image from a directory;

    public static IHtmlString Image(this HtmlHelper helper, string src, string alt, int whichpic,string width,string height)
    {
       switch (whichpic)
       {
           case 1:
                src = "~/assets/img/products/" + src + "_1.jpg";
                break;
           case 2:
                src = "~/assets/img/products/" + src + "_2.jpg";
                break;
           case 3:
                src = "~/assets/img/products/" + src + "_3.jpg";
                break;
       }


        TagBuilder tb = new TagBuilder("img");
        tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
        tb.Attributes.Add("alt",alt);
        tb.Attributes.Add("width", width);
        tb.Attributes.Add("height", height);

        return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
    }

The problem is that, the width and height attribute doesn't work.

Can anyone help me to create width and height styles attribute?

Thank you

4

0 回答 0