小编典典

使用Ajax渲染部分视图

ajax

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

如何实现?

非常感谢您阅读本文。


阅读 220

收藏
2020-07-26

共1个答案

小编典典

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

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

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

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

$(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);
          }
    });
});
2020-07-26