Display Image from DB in Modal Bootstrap Window

Rob Bowman

I'm using ASP.net MVC5 with Bootstrap 3 I have the following View

@model WilhanWebsite.Models.PhotoViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Gallery</h2>

<p>
    @Html.ActionLink("Upload Photo", "Create")

    <br/>


    @for (var i = 0; i < Model.DownloadedImages.Count; i++)
    {
        <a data-toggle="modal" data-target="#img-thumbnail@i" class="img_modal" href="data:image/jpg;base64,@(Convert.ToBase64String(Model.DownloadedImages[i].ImageData))">
            <img id="img-thumbnail@i"  class="img-thumbnail" src="data:image/jpg;base64,@(Convert.ToBase64String(Model.DownloadedImages[i].ImageData))" alt="@Model.DownloadedImages[i].Description" />
        </a>
    }

    <div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Title to go here</h3>
        </div>
        <div class="modal-body">
            <img id="modal_img_target">
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        </div>
    </div>

</p>

When I click an image from the gallery I would like it to display in a modal window.

My problem is, when I click the link, the modal window displays without the image. All the examples I've seen use images from the file system whereas my site uses images from a db via a byte array. I guess the fix may be to change the line of javascript that is assigning the source attribute on the modal image but I've tried various things here without a result. The problem can be seen at the following address: Problem page

** EDIT ** Updated view to use for loop to identify images as suggested. Problem changed, now rather than displaying a blank modal page, the image is displayed on a blank page.

** EDIT Working View **

 @model WilhanWebsite.Models.PhotoViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Gallery</h2>

<p>
    @Html.ActionLink("Upload Photo", "Create")

    <br/>


    @for (var i = 0; i < Model.DownloadedImages.Count; i++)
    {
        <a data-toggle="modal" data-target="#img-thumbnail@i" class="img_modal" href="data:image/jpg;base64,@(Convert.ToBase64String(Model.DownloadedImages[i].ImageData))">
            <img id="img-thumbnail@i"  class="img-thumbnail" src="data:image/jpg;base64,@(Convert.ToBase64String(Model.DownloadedImages[i].ImageData))" alt="@Model.DownloadedImages[i].Description" />
        </a>
    }

    <div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Title to go here</h3>
        </div>
        <div class="modal-body">
            <img id="modal_img_target">
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        </div>
    </div>

</p>


<script type="text/javascript">
    $('.img_modal').on('click', function (e) {
        e.preventDefault();
        $("#modal_img_target").attr("src", this);
        $("#modal").removeClass("hide");
        $('#modal').modal('show');
    });
</script>
hutchonoid

You need to add the toggle and target to the link:

<a ... data-toggle="modal" data-target="#img-thumbnail1"

Then add the target id to the image:

<img id="img-thumbnail1" class="img-thumbnail" 

This means that you will have to add an incrementing number to the loop so that id's are unique.

You can do this like this:

@for (var i = 0; i < Model.DownloadedImages.Count; i++)
{
    <a data-toggle="modal" data-target="#img-thumbnail@i" class="img_modal" href="data:image/jpg;base64,@(Convert.ToBase64String(photo.ImageData))">
        <img id="img-thumbnail@i"  class="img-thumbnail" src="data:image/jpg;base64,@(Convert.ToBase64String(photo.ImageData))" alt="@photo.Description" />
    </a>
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Disallow Twitter Bootstrap modal window from closing

From Dev

prevent bootstrap modal window from closing on form submission

From Dev

Display Modal window in Xamarin

From Dev

Issues with Bootstrap Modal Window

From Dev

Display data from code behind in jQuery Modal Window

From Dev

How to close bootstrap modal window from angularjs

From Dev

Pass data to parent window from modal using Bootstrap

From Dev

Close angular bootstrap modal window from a controller

From Dev

Display image from DB on zk zul listbox

From Dev

AngularJS can't pass $index from bootstrap modal window

From Dev

pass a value from mysql db to bootstrap modal

From Dev

Bootstrap how to show data from row in modal window

From Dev

Remove modal property from Bootstrap modal window

From Dev

How do i display image in bootstrap modal with Javascript

From Dev

Bootstrap modal cuts from left when window resize

From Dev

Stop bootstrap modal from closing when outside the window is clicked?

From Dev

Issues with Bootstrap Modal Window

From Dev

Prevent Bootstrap modal window from opening in new tab / window

From Dev

How to close bootstrap modal window from angularjs

From Dev

Close angular bootstrap modal window from a controller

From Dev

Angularjs - To pass scope, from modal bootstrap window

From Dev

Bootstrap : Get Image from modal

From Dev

Display a Modal Popup window

From Dev

Same Modal Bootstrap but different info from DB deppending on ID?

From Dev

how to hide Bootstrap modal window if from field is not valid

From Dev

Edit db record with modal window

From Dev

Bootstrap : Show modal window on image click in a loop

From Dev

Make a countdown timer display a modal window bootstrap

From Dev

Why does my Bootstrap Modal window doesn't work on php? When I try to open the modal window it won't display

Related Related

  1. 1

    Disallow Twitter Bootstrap modal window from closing

  2. 2

    prevent bootstrap modal window from closing on form submission

  3. 3

    Display Modal window in Xamarin

  4. 4

    Issues with Bootstrap Modal Window

  5. 5

    Display data from code behind in jQuery Modal Window

  6. 6

    How to close bootstrap modal window from angularjs

  7. 7

    Pass data to parent window from modal using Bootstrap

  8. 8

    Close angular bootstrap modal window from a controller

  9. 9

    Display image from DB on zk zul listbox

  10. 10

    AngularJS can't pass $index from bootstrap modal window

  11. 11

    pass a value from mysql db to bootstrap modal

  12. 12

    Bootstrap how to show data from row in modal window

  13. 13

    Remove modal property from Bootstrap modal window

  14. 14

    How do i display image in bootstrap modal with Javascript

  15. 15

    Bootstrap modal cuts from left when window resize

  16. 16

    Stop bootstrap modal from closing when outside the window is clicked?

  17. 17

    Issues with Bootstrap Modal Window

  18. 18

    Prevent Bootstrap modal window from opening in new tab / window

  19. 19

    How to close bootstrap modal window from angularjs

  20. 20

    Close angular bootstrap modal window from a controller

  21. 21

    Angularjs - To pass scope, from modal bootstrap window

  22. 22

    Bootstrap : Get Image from modal

  23. 23

    Display a Modal Popup window

  24. 24

    Same Modal Bootstrap but different info from DB deppending on ID?

  25. 25

    how to hide Bootstrap modal window if from field is not valid

  26. 26

    Edit db record with modal window

  27. 27

    Bootstrap : Show modal window on image click in a loop

  28. 28

    Make a countdown timer display a modal window bootstrap

  29. 29

    Why does my Bootstrap Modal window doesn't work on php? When I try to open the modal window it won't display

HotTag

Archive