加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MVC,与AJAX / JQUERY异步显示图像列表

发布时间:2020-12-16 02:57:07 所属栏目:百科 来源:网络整理
导读:我有一个网站,我从数据库中加载图像列表(缩略图).问题是图像显示得相当慢,因为使用Url.Action获取每个缩略图相当耗时(当通过整个MVC管道时). 因此,我想用Ajax amp;异步加载图像. JQUERY,同时显示每个图像的标准加载图像(ajaxloag.info),直到加载图像.类似的
我有一个网站,我从数据库中加载图像列表(缩略图).问题是图像显示得相当慢,因为使用Url.Action获取每个缩略图相当耗时(当通过整个MVC管道时).

因此,我想用Ajax& amp;异步加载图像. JQUERY,同时显示每个图像的标准加载图像(ajaxloag.info),直到加载图像.类似的问题已经提出here,但我需要一个更完整的例子,因为我对MVC和JQUERY很新.

提前致谢,

查看(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=""  />

调节器

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>

解决方法

您可以使用HTML5 data- *属性:

<img width="100" 
     src="ajax-loader.gif" 
     class="async" 
     data-img-url="<%= Url.Action("Thumbnail",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",new { productId = Model.ID }) %>" 
/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读