This is a bit of a rant, but this fundamental misunderstanding is so widespread, I felt it needed to be addressed. As a lurker for the
asp.net-mvc* tag on StackOverflow, I see an endless barrage of questions to the effect of "How do I refresh a partial view without reloading the page?" This is essentially nonsense. You may not realize it's nonsense, but it is. What you're really trying to ask, if you've asked a question like this, is "How do I use AJAX to request a bit of HTML from the server to replace a portion of the DOM with?" That may not all make sense to you, right now, but let's break it down.
First, a "partial view" is a concept from ASP.NET MVC that merely means a view without a layout. A view, again in the parlance of ASP.NET MVC, is a file with the extension
.vbhtml (depending on the language; C# or VB, respectively; being used) that is used by Razor (the default ASP.NET MVC templating engine) to render a response for a controller action that returns a view result (
PartialViewResult). That's a little heady, but the long and short is that a "partial view" is only a thing, server-side.
Once a response is sent to the client, all you have is an HTML document. Client-side, it doesn't matter whether you used a partial view, no partial views, or a 100 partial views. The client receives an HTML document, and in the case of the most typical web client, a web browser, it renders that document and presents it to the user. Part of that rendering process involves creating the DOM, or Document Object Model, for the web page. The browser parses all the different HTML tags in the response and creates a hierarchical structure that represents the document being viewed by the user. This DOM is all that exists after the browser does its work. You technically don't even have HTML any more at this point, just the browser's internal representation of it.
In the simplest of cases, where the intent is merely to grab a new bit of HTML from the server to replace some existing page content, an AJAX request is made to a server. In the parlance of ASP.NET MVC, the particular resource requested would be an action that returns a partial view (
There's a million and one tutorials detailing the exact procedures and code to make all that happen; that's not the purpose of this post. My goal is here is explain the high-level concepts of what's happening. I feel that entirely too few web developers actually understand how the web (and the Internet, in general, for that matter) works, and this is a very grievous problem in the community. I love that the web development community is so accepting. One of the beautiful and inspiring things about the web is how open it is and how low a barrier to entry it has. That said, I think we as a community tolerate a sustained level of ignorance that other communities would not. If you wanted to develop an Android application and never bothered to learn anything about how Android as a platform actually functions, the Android development community would have no problem telling you that you needed to go do your homework - learn the platform - first, before attempting to run off and develop an Android app. However, there's this pervasive assumption in the web development community that you don't need to understand the web or the Internet as a platform in order to develop web applications. That's both foolish and dangerous.
Hopefully, this will help those that stumble across it learn a little bit more about the platform they aspire to develop for, and to those: I encourage you to seek out similar resources to learn more about the web and the Internet, in general. Having a good working knowledge of the platform your web application runs on will not only make you a better and more competent developer but it will also aid you in figuring out solutions to problems that may arise along the way.