Calling a Partial View

A partial view can be called using the @Html.Partial HTML helper.

In our case, we will be calling the partial view from the Index2.cshtml file. The parameter that you pass will be the name of the partial file. It will search for the partial view by that name and render that complete content as part of the Index2.cshtml file.

The content of the Index2.html file will now be as follows:


Go to https://goo.gl/1nRe4M to access the code.
Hello. This text will be rendered in body of the layout page<br />
<br />
<br />
@Html.Partial("_PartialHelloWorld")

Now, run the application and access the URL http://localhost:50132/Home/Index2. You'll see the following output:

Also, look at the other overloads of @Html.Partial by right-clicking and selecting Peek Definition. This will allow you to pass a model or a copy of ViewBag. We say a copy because changes to the bag within the partial view do not propagate back to the parent.