What is the difference between returning a View, a PartialView and JSON from an action?
Quick Answer
View() renders a full page (content + layout). PartialView() renders just the view markup with no layout — for injecting a fragment into an existing page via AJAX. Json() serialises an object to JSON — for API-style calls or client-side rendering.
View() gives back a whole page — it runs through _ViewStart and gets wrapped in the layout. PartialView() returns only that view's markup with no layout, which is what I return from an action that an AJAX request calls to swap out a section of the page — a results list, a modal body. Json() just serialises an object, for when the browser is doing the rendering. In an actual Web API I'd return Ok(obj) and let content negotiation produce the JSON.
- A full
Viewruns_ViewStartand wraps the content in the layout. - A
PartialViewskips the layout — return it from an action that an AJAX call hits to refresh a section (a search results list, a modal body). Jsonis for when the client does the rendering (a JS framework) or for a lightweight endpoint. In Core Web API you'd usually useOk(obj)and content negotiation instead.- A common pattern: one action returns a full
Viewon a normal request and aPartialViewwhenRequest.Headers["X-Requested-With"]indicates AJAX.