【问题标题】:MVC, display a list of images asynchronously with AJAX / JQUERYMVC,用AJAX/JQUERY异步显示图片列表
【发布时间】:2011-02-24 06:17:06
【问题描述】:

我有一个站点,我在其中从数据库加载图像列表(缩略图)。问题是图像的显示速度很慢,因为使用 Url.Action 获取每个缩略图相当耗时(在遍历整个 MVC 管道时)。

因此,我想使用 Ajax 和 JQUERY 异步加载图像,同时显示每个图像的标准加载图像 (ajaxloag.info),直到图像被加载。 here 提出了一个类似的问题,但我需要一个更完整的示例,因为我对 MVC 和 JQUERY 非常陌生。

提前致谢,

视图(部分视图)

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

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

控制器

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 - 包含 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>

【问题讨论】:

    标签: jquery asp.net-mvc ajax


    【解决方案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 }) %>" 
    />
    

    【讨论】:

    • +1 好答案。在设置新 url 之前,您有什么理由等待 ajax 加载器图像到 load
    • @Josiah Ruddell,让用户有机会在加载实际图像时看到微调器图像。
    • @Darin,我真的无法让它工作。它没有击中我在指定控制器上的操作。您的示例中是否缺少某些内容? :)
    • @Nima,FireBug 说了什么?你能看到正在发送的图像请求吗?您是否在页面中包含 jquery?你输入.loadjavascript回调了吗?
    • @Darin,不,我看不到正在发送的请求,所以我一定错过了一些东西。实际上,上面的视图 sn-p 是在 PartialView 中,所以 Javascript 函数到目前为止被复制到父视图(启动 partialView 的那个)。我正在使用 JQUERY 加载产品列表,所以我认为 JQUERY 包含正确。如何确保输入了 .load javascript 回调? (我对这个 JQUERY 东西很陌生)
    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多