MVC Partials with fetch
MVC Partials with fetch: MVC Partials with fetch: an overview, Returning a Partial View from an Action, Using the fetch API.
Updated
- MVC Partials with fetch: an overview (Journeyman): An MVC page can reload just one part of itself. JavaScript fetches the new part's HTML from an action that returns a partial view, and swaps it in. That's the MVC way to do what an API and JSON do for a JavaScript-built page.
- Returning a Partial View from an Action (Journeyman): Move the part of the page that changes into a partial view, render it on the full page with the partial tag helper, and add an action that returns just that partial with PartialView, so a request gets only its HTML, without the layout.
- Using the fetch API (Journeyman): Catch the pager's clicks with one listener on the pager, cancel them with preventDefault, fetch the partial's HTML with fetch and response.text, and put it in the page with innerHTML. The links keep working when JavaScript doesn't run.