2014-03-19 33 views
1

我正在使用mvc4,Razor和SignalR。現在在我的cshtml中。我有ASP.NET,MVC Razor - 如何顯示腳本中的圖像

<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" />'; 
} 
} 

問題是,我什麼都試過,但它從來沒有顯示圖像僅alt文本。 如果我把相同的標籤放在腳本的外部,它可以正常工作,這意味着路徑沒問題。只是在腳本中它有一個問題。嘗試了src上的url.content,但沒有運氣。有什麼建議麼?

回答

1

嘗試使用

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


這樣的..

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

合作。非常感謝。 – asb

2

src存在問題。檢查URL

這可能是

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