【问题标题】:Better solution to location.hreflocation.href 的更好解决方案
【发布时间】:2015-06-05 13:47:09
【问题描述】:

我有下表:

这基本上是在.cshtml 中使用foreach 和<tr><td> 附加的。当我单击一个文件夹时,它应该让我获得内容并用新内容重新加载表格。我使用.ajax 获取内容,并使用window.location.href 重新加载页面。但是,使用location.href 重新加载页面确实需要很多时间。页面的性能也很差,因为它再次调用location.href 上的服务器端代码。对于这种情况,您有什么建议?

【问题讨论】:

  • 如何处理客户端缓存?
  • 为什么要重新加载页面?
  • @Curt:我将如何刷新同一张桌子上的内容?新内容是从服务器拉取的。
  • @Heru-Luin:如果你的意思是.ajaxcache,那么我没有特别提到ajax请求,所以默认可能是true

标签: jquery asp.net ajax asp.net-mvc


【解决方案1】:

你说了一些关于 .cshtml 的事情,所以我将尝试向你解释我是如何在 asp.net mvc 中做到这一点的。

您说您正在使用 ajax 调用。没关系。

请创建一个局部视图 (http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC),它将用于为您提供文件视图的 html。在您的操作中(您从 ajax 调用)

public JsonResult RenderFilesView(int renderMyFolderId){
  var folderModelWithFiles = this._repository.GetFolderAndFilesModel(renderMyFolderId);

  var filesViewHtml = this.RenderRazorViewToString("_FilesView", folderModelWithFiles);

  return Json(new { FilesViewHtml = filesViewHtml });
}

RenderPartialViewToString 在这里:Render partial view to string MVC4

并且你会在你成功调用 ajax 的函数中收到 FilesViewHtml。

$.ajax( url: 'godKnows/RenderFilesView', 
        success: function(response){

               // access response.FilesViewHtml 
               // And you can do something like: 
               $('#container').html(response.FilesViewHtml);
        });

并且无需重新加载页面或天知道是什么。

【讨论】:

  • 我知道这种方法,但不知何故我一直在避免它,因为在 c# 中生成了难看的 HTML 字符串。但是,这似乎是满足此要求的唯一方法......无论如何感谢您的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-15
相关资源
最近更新 更多