更好的解决方案location.href

时间:2015-06-05 13:47:10

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

我有以下表格: folder table

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

1 个答案:

答案 0 :(得分:0)

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

你说你正在使用ajax电话。没关系。

请创建部分视图(http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC),该视图将用于为您提供html for files视图。在你的行动中(你从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);
        });

无需重新加载页面或上帝知道什么。