I am trying to change the source of an image dynamically using jQuery. Here's an example of what we were doing before:
$('#resultsDiv').on('mouseover', '.cardImage', function() {
var imageSrc = $(this).attr("data-id");
imageSrc = imageSrc.replace("~", "");
$('#cardImageDiv').attr('src', imageSrc);
});
The imageSrc
variable was storing the physical location of an image; however, we must change that as it is no longer sure we might have access to the physical location of an image on our server. So we've stored our image in a database and I'm trying to get it back like this:
$('#resultsDiv').on('mouseover', '.cardImage', function () {
var cardID = $(this).attr("data-id");
alert("woah!");
alert(cardID);
$.ajax({
type: "GET",
url: '@Url.Action("UpdateImageByCardID","Images")',
data: {_cardid: cardID},
datatype: "image/png",
success: function (data) {
alert("yeah!");
$('#cardImageDiv').attr('src', data);
}
});
});
The cardId
is the unique identifier of the image. Here's the Images
controller action:
public FileResult UpdateImageByCardID(int? _cardid)
{
byte[] imageData = mImageManager.GetNormalImageAsyncByCardId((int)_cardid);
return File(imageData, "image/png");
}
Now I know the action is hit in debugging and returns a correct byte[]
value, however the image src is not updated.
For information only here's how the first image is displayed:
<img id="cardImageDiv" src="@Url.Action("GetNormalImageByCardIdAsync", "Images", new { @_cardId = firstSrc.CARD_IDE})" alt="@firstSrc.CARD.CARD_NAME" title="@firstSrc.CARD.CARD_NAME" class="nullify"/>
The GetNormalImageByCardIdAsync
is an asynchronous file returned by the controller task and it does its job. Now I wanna know what's causing the problem and how can I fix it?