0

Cutting right to the chase, I've got something like this in my Controller:

return base.File(getSomeImageBitmap, "image/jpeg");

Which displays the image just fine in a new window using @Html.ActionLink. However, I want to populate the image directly into the table when a link is clicked. So, I tried this approach:

@Ajax.ActionLink("View", "Image", "Controller", new { id = t.id, id2 = t.id2}, 
                    new AjaxOptions { UpdateTargetId = "myImage", HttpMethod = "GET" }
<div id="myImage"></div>

Given t is an object in the Model Array, this approach has two downsides. First, it displays the un-encoded bitmap as text, not an image. Second, it populates the data into the first div in the table every time.

My next strategy will be to create a PartialView and generate unique DIV ids so they can be inserted using @Ajax.ActionLink, but I have no idea if I can make it work like that.

Finally, I've tried:

 <img src="@Url.Action("Image", "Controller", new { id = t.id, id2 = t.id2 })" alt="Some Image" />

Which works perfectly, minus the fact that it queries my webservice for up to 500 images all at once. I can't have that happen, I only want to retrieve the image when some arbitrary link is clicked. Is this possible? Jquery and Ajax are all fair game.

EDIT: Went with the following solution, which supports hiding the image as well. It only lacks any kind of loading text:

<img hidden src="" alt="No Image" class="imagePreview" />
<script type="text/javascript">
    // Handle clicking the View image link
    $(".linkView").click(function (e) {
        e.preventDefault();
        // The link being clicked
        var _this = $(this);
        // Find the closest imagePreview to the link
        var image = _this.closest("tr").find("img.imagePreview");

        if (image.is(':visible')) {
            // If the image is visible, hide it
            _this.text("View");
            image.hide();
        } else {
            // If image is not visible, show it
            _this.text("Hide");
            image.show();

            // If the image src has not been set, set it to the link href
            // By not clearing the src attribute, we can show/hide without loading the image again
            if (image.attr("src") == "") {
                image.attr("src", _this.attr("href"));
            }
        }
    });
</script>

1 Answer 1

1

The problem is, your code is going to generate more than one div with id "myImage". that is not valid HTML. Id values should be unique.

I suggest you use a normal link and write your one jQuery code to handle the loading of the image when the link is clicked.

 @Html.ActionLink("View", "Image", "Home",  new { id = t.id, id2 = t.id2},
                                                                  new {@class="linkView"})
 <img class="imagePreview"/>

This will render a normal link with css class linkView and and an image tag with css class imagePreview.

Now listen to the click event on these link, prevent the default click behavior ( navigating to the href value), update the image src attribute.

You may use the closest() method to get a reference to the current table row (where the link belongs to ) and use find() method to get a reference to our image.

$(function(){

   $(".linkView").click(function(e){
        e.preventDefault();
        var _this=$(this);
        _this.closest("tr").find("img.imagePreview").attr("src",_this.attr("href"));

   });

});
Sign up to request clarification or add additional context in comments.

2 Comments

This is the approach I was hoping to take, but was missing the appropriate jquery knowledge. I'll give it a try.
Worked like a charm. I added the ability to show/hide the image and posted my code to the original question. In the mean time I'd like to find a way of showing a loading indication of some kind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.