使用 ajax 渲染局部视图 [英] Rendering Partial Views using ajax

查看:27
本文介绍了使用 ajax 渲染局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了这个问题 它解决了我最初的问题.但是我不希望仅在用户单击链接时才呈现局部视图,我想在页面加载时呈现局部视图,并且可能在加载局部视图时显示进度指示器.

I've checked this question and it solved my initial problems. But I don't want the partial view to be rendered only when the user clicks a link, I want to render partial views when the page is loaded and, possibly, show a progress indicator while the partial view is being loaded.

如何实现?

非常感谢您阅读本文.

推荐答案

如果您想加载页面,然后通过 ajax 加载部分视图,您可以创建一个 ActionMethod 执行以下操作:

If you want to load the page and then load the partial view via ajax you could create an ActionMethod that does something like:

public ActionResult Create()
{
     var model = new MyModel();

     if (Request.IsAjaxRequest())
     {
          return PartialView( "_Partial", model.PartialModel );
     }
     else
     {
          return View( model );
     } 
}

然后在您的页面中执行以下操作:

and then in your page do something like:

$(function(){

    $(document).ajaxStart(function () {
        //show a progress modal of your choosing
        showProgressModal('loading');
    });
    $(document).ajaxStop(function () {
        //hide it
        hideProgressModal();
    });

    $.ajax({
          url: '/controller/create',
          dataType: 'html',
          success: function(data) {
             $('#myPartialContainer').html(data);
          }
    });
});

这篇关于使用 ajax 渲染局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆