在 ASP.NET MVC 2 中呈现 HTML img 标签的正确语法是什么?

发布于 2024-10-21 10:21:27 字数 331 浏览 2 评论 0原文

<img alt="Upload Profile Pic" src="<%= ViewData["PicSource"].ToString() %>" />

更新: 另外,当我们编写此语法时,Visual Studio 用红色卷线强调了 html 代码的几个元素,就像我们遇到一些语法错误时一样,并且看起来我们编写的语法不会工作并且是错误的,但实际上当我构建它时,它工作了。

它还拒绝自动格式化 html 代码。这对我来说是一个足够充分的理由来问这个问题,因为尽管我知道这种语法会起作用,但每次我这样写时,我还是觉得我做错了什么。

<img alt="Upload Profile Pic" src="<%= ViewData["PicSource"].ToString() %>" />

UPDATE: Also, when we write this syntax, visual studio underlines several elements of our html code with curly red lines just like when we have some syntax errors and it looks like that the syntax we have written will not work and is wrong, but actually when I built it, it worked.

It also denies to auto format the html code. This was an enough good reason for me for asking this question because even though I knew that this syntax will work, still then when everytime I write like this, I feel that I have done something wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-28 10:21:27

是的,你可以这样写。

然而,它通常被称为“意大利面条代码”,最佳实践是使用辅助方法。

public static class ImageHelper
{
    public static MvcHtmlString Image(this HtmlHelper html, string sourcePath, object htmlAttributes)
    {
        return html.Image(sourcePath, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString Image(this HtmlHelper html, string sourcePath, IDictionary<string,object> htnlAttributes)
    {
        if(string.IsNullOrEmpty(sourcePath))
        {
            throw new ArgumentException("Image source path cannot be null or empty.", "sourcePath");
        }
        TagBuilder tagBuilder = new TagBuilder("img");
        tagBuilder.MergeAttributes<string,object>(htnlAttributes);
        tagBuilder.MergeAttribute("src", sourcePath, true);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
    }
}

然后,你使用的看起来像

        Html.Image(ViewData["PicSource"] as string, new {alt = "Upload Profile Pic"});

Yes, you can write like that.

However, it is often referred to as "spaghetti code", and the best practice would be to use a helper method.

public static class ImageHelper
{
    public static MvcHtmlString Image(this HtmlHelper html, string sourcePath, object htmlAttributes)
    {
        return html.Image(sourcePath, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString Image(this HtmlHelper html, string sourcePath, IDictionary<string,object> htnlAttributes)
    {
        if(string.IsNullOrEmpty(sourcePath))
        {
            throw new ArgumentException("Image source path cannot be null or empty.", "sourcePath");
        }
        TagBuilder tagBuilder = new TagBuilder("img");
        tagBuilder.MergeAttributes<string,object>(htnlAttributes);
        tagBuilder.MergeAttribute("src", sourcePath, true);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
    }
}

then, you're use looks like

        Html.Image(ViewData["PicSource"] as string, new {alt = "Upload Profile Pic"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文