【问题标题】:ASP .NET, MVC Razor - How to display image from scriptASP .NET,MVC Razor - 如何从脚本显示图像
【发布时间】:2014-04-25 17:31:08
【问题描述】:

我正在使用 mvc4、Razor 和 SignalR。现在在我的cshtml中。我有

<div>
<ul id="friends"></ul>
</div>

在我的脚本中

$(function () {

//code omitted

// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;

chat.client.addfriend = function (name) {
// Add the message to the page.
$('#friends').append('<li><strong>' + htmlEncode(name)
+ '</strong> ' + getOnlineIcon() + '</li>');
};

function getOnlineIcon() {
return '<img id="img_logo" alt="test" src="~/Content/Images/greenDot.gif" />';
}
}

问题是我尝试了所有方法,但它从不只显示替代文本的图像。 如果我在脚本外部放置相同的标签,它可以正常工作,这意味着路径没问题。只是在脚本中有问题。也尝试了 src 上的 url.content ,但没有运气。有什么建议吗?

【问题讨论】:

    标签: asp.net-mvc razor signalr-hub


    【解决方案1】:

    src 有问题。检查网址

    可能是

     src="/Content/Images/greenDot.gif"
    

    【讨论】:

      【解决方案2】:

      尝试使用

      @Url.Content("~/Content/Images/greenDot.gif")
      


      像这样..

      function getOnlineIcon()
      {
          return '<img id="img_logo" alt="test" src="' + 
              '@Url.Content("~/Content/Images/greenDot.gif")' + 
              '" />';
      }
      

      【讨论】:

      • 工作。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2023-04-01
      • 2016-08-04
      • 1970-01-01
      相关资源
      最近更新 更多