Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ajax渲染局部视图_C#_Jquery_Asp.net Mvc_Ajax_Razor - Fatal编程技术网

C# 使用ajax渲染局部视图

C# 使用ajax渲染局部视图,c#,jquery,asp.net-mvc,ajax,razor,C#,Jquery,Asp.net Mvc,Ajax,Razor,我检查过了,它解决了我最初的问题。但我不希望仅当用户单击链接时才呈现部分视图,我希望在加载页面时呈现部分视图,并且可能在加载部分视图时显示进度指示器 如何做到这一点 非常感谢您阅读本文。您可以通过编写@Html.Partial(…)控制器在初始页面中呈现它 public ActionResult GetModule(string partialName){ return PartialView("~/Views/Shared/"+partialName); } 在默认页面上(使用jqu

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

如何做到这一点


非常感谢您阅读本文。

您可以通过编写
@Html.Partial(…)
控制器在初始页面中呈现它

public ActionResult GetModule(string partialName){
    return PartialView("~/Views/Shared/"+partialName);
}
在默认页面上(使用jquery ajax操作)

正在加载。。。
$(“#mod1”).load(“/ControllerName/GetModule?partialName=test1”);

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

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);
          }
    });
});
然后在页面中执行以下操作:

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);
          }
    });
});

我是否需要将
部分视图的文件格式设置为
?比如.cshtmlhink@Html.Parital不能在Ajax、jquery中使用。@Shan:这是服务器端的东西;这与此无关。@SLaks,似乎他需要在主视图显示后显示部分视图(可能是为了避免长时间的初始处理)。据我所知,您建议的@Html.Partial()将与主视图一起在服务器上处理。所以它不能解决人类的问题。我最初也被这个问题所欺骗。