-2

I have this following table: folder table

This is basically loaded using foreach in .cshtml with <tr><td> append. When I click on a folder, it should get me the contents and reload the table with new contents. I use .ajax to get the contents, and use window.location.href to reload the page. However it takes really lot of time to reload the page using location.href. Also the performance of page is poor, since it again calls the server side code on location.href. What do you suggest for such a situation?

Saroj
  • 207
  • 2
  • 5
  • 14

1 Answers1

0

You said something about .cshtml so i will try to explain you how i do this in asp.net mvc.

You said you're using an ajax call. That's ok.

Please create a partial view (http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC), which will be used to bring you the html for files view. And inside your action (which you call from ajax)

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

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

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

RenderPartialViewToString here: Render partial view to string MVC4

And you will receive in your success function of ajax call the FilesViewHtml.

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

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

And no need to reload the page or god knows what.

Community
  • 1
  • 1
Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
  • I knew this approach, but somehow I have been avoiding it because of generating ugly strings of HTMLs in c# . However, it seems like that's the only way to do this requirement... anyways thanks for your answer :) – Saroj Jun 05 '15 at 14:25