MVC, displays a list of images asynchronously with AJAX / JQUERY

I have a website where I download a list of images (thumbnails) from a database. The problem is that the images are displayed quite slowly, since fetching each sketch using Url.Action is quite time-consuming (when passing through the entire MVC pipeline).

Therefore, I would like to upload images asynchronously with Ajax and JQUERY, showing the standard boot image (ajaxloag.info) for each image until the image is loaded. A similar question was raised here , but I need a more complete example, since I am very new to MVC and JQUERY.

Thanks in advance,

View (partialView)

// Foreach product, display the corresponding thumbnail
<% foreach (var p in Model)
   { %>
.
.

    <img width="100" src="<%= Url.Action( "Thumbnail", "Products", new { productId = p.ID } ) %>" alt=""  />

controller

public ActionResult Thumbnail(string productId)
        {
            try
            {
                Guid pid = new Guid(productId);
                byte[] thumbnailData = _productsRepository.GetProductThumbnail(pid);
                if (thumbnailData != null)
                {
                    return File(thumbnailData, "image/jpg");
                }
                else
                {
                    return File(@"../Content/missingproduct.png", "image/jpg");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

UPDATE - parent view containing Javascript

<script type="text/javascript">

    $(document).ready(function() 
    {
       getContentTab (1);
    });

    function getContentTab(index)
     {
        var criteria = {
            categoryId : "<%: ViewBag.Header %>", 
            tabIndex: index, 
            searchString : "<%: ViewBag.SearchString %>"
            };
        var url='<%= Url.Content("~/Products/GetAjaxTab") %>'
        var targetDiv = "#listContent"; 
        var ajaxLoadUrl = '<%: Url.Content("/Content") %>/ajax-loader.gif';
        var ajaxLoading = "<img id='ajax-loader' src='" + ajaxLoadUrl + "' align='left' height='28' width='28' />";


        $(targetDiv).html("<div>" + ajaxLoading + " Loading...</div>");

tag in respect to the callback. 
        $.get(url, criteria, function(result)
        {
            $(targetDiv).html(result);
        });

    }


    function AjaxStart()
    {
        $('#listContent').mask('Opdaterer');
    }

    function AjaxEnd()
    {
        $('#listContent').unmask();

    }

    $(function () {
        $('.async').load(function () {
            $(this).unbind('load');
            this.src = $(this).attr('data-img-url');
            alert('2');
        });
    });

//]]>


</script>
+5
1

HTML5 data-*:

<img width="100" 
     src="ajax-loader.gif" 
     class="async" 
     data-img-url="<%= Url.Action("Thumbnail", "Products", new { productId = p.ID }) %>" 
/>

js:

$(function() {
    $('.async').load(function() {
        $(this).unbind('load');
        this.src = $(this).attr('data-img-url');
    });
});

/ foreach :

<%= Html.DisplayForModel() %>

:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Product>" 
%>
<img width="100" 
     src="ajax-loader.gif" 
     class="async" 
     data-img-url="<%= Url.Action("Thumbnail", "Products", new { productId = Model.ID }) %>" 
/>
+10

All Articles