3

I've noticed there are several ways to use Views and PartialViews:

RenderAction, RenderPartial, and "return PartialView"

RenderAction when placed inside HTML, will simply call an Action and Render the View returned (the View returned can be partial view or view?)

RenderPartial will simply retrieve the contents of a View without executing any Controller action

Finally, what's the difference between "Return View" and "Return PartialView"?

Thanks

1 Answer 1

12

return View() returns the view with a Layout enabled so you get full HTML page with <html> and <body> tags. return PartialView() on the other hand disables the Layout and you get only the HTML fragment contained in this view. Actually when working with the Razor view engine I prefer to talk about templates and not views and partial views. That's because a view is a template and a partial view is a template without a layout. But in both cases it's a Razor template.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand its a matter of preference and need? When there is an Ajax request, better use "Return PartialView"? Thanks
@bhaidar, when there is an AJAX request it will depend on what you intend to do with the results. If you intend to replace some portion of the DOM with the HTML fragment returned by the controller action then, yes, you should return PartialView to ensure that there is no Layout included or you will end up with invalid HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.