csdn产品小助手 2014-07-20 14:49 采纳率: 0%
浏览 58

使用AJAX切换部分视图

In my MVC-project I have this code for rendering a partial-view:

Method:

public ActionResult ShowArtCollection()
        {
            var model = new ViewModel();
            model.ArtWorks = db.ArtWorks.ToList();

            return PartialView("_artcollection", model);
        }

AJAX:

$("#btnArt").click(function () {

        $.ajax({
            url: '/Home/ShowArtCollection',
            dataType: 'html',
            success: function (data) {
                $('#artworks').html(data);
            }
        });
    });

I would like my #btnArt to be able to toggle the partial view. I mean that when the _artcollection is rendered by the click of the button, the next click should "unrender" the view. Any tips on how to achieve this?

  • 写回答

1条回答 默认 最新

  • weixin_33714884 2014-07-20 15:05
    关注

    you can put a flag and check if rendered next time unrednder on click:

    var rendered = false;
    $("#btnArt").click(function () {
    
        if (!rendered) {
    
            $.ajax({
                url: '/Home/ShowArtCollection',
                dataType: 'html',
                success: function (data) {
                    $('#artworks').html(data);
                    rendered = true;
                }
            });
        } else {
            $('#artworks').html("");
            rendered = false;
        }
    
    });
    

    this will do the trick for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作