1

I know the difference between View and PartialView, but Is there any difference here in below code (as both return the view with expected result),

1.

public ActionResult Index()
    {
        return View();
    }

2.

public ActionResult Index()
    {
        return PartialView();
    } 
user584018
  • 10,186
  • 15
  • 74
  • 160

2 Answers2

5

If you specified Layout property inside your content view there is no difference. When returning View(), ViewStart.cshtml executed but when returning PartialView(), ViewStart.cshtml was not executed.So when view does not set Layout property, return View() can take Layout from Viewstart.cshtml but return PartialView() cannot.For more information Rendering difference between PartialView() and view()

If your content.cshtml contains this code block they are same and generate view wrapped in specified layout.

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

If Content view does not specify any Layout, return View() gets its Layout property from ViewStart.cshtml and generates a view wrapped in layout. But return PartialView() only generates the content view because Viewstart.cshtml is not executed when you return PartialView().

Community
  • 1
  • 1
Mustafa ASAN
  • 3,747
  • 2
  • 23
  • 34
0

View will return layout with view content. Partial returns only content

Piotr Dory
  • 334
  • 2
  • 12