Table of contents
1.
Introduction
2.
Use of ASP.NET Razor Partial View
3.
Creating an ASP.NET Razor Partial View
4.
Rendering ASP.NET Razor Partial View
4.1.
@Html.Partial()
4.2.
@Html.RenderPartial()
4.3.
@Html.Action()
4.4.
@Html.RenderAction()
5.
Working Example
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET Razor Partial View

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ASP.NET delivers a facility to create reusable components that we can share in the web application. These sharable elements are known as partial views.

In ASP.NET Razor Partial Views are a reusable part of a web page. It is a .cshtml or .vbhtml file that contains HTML code. We can use it in one or more Views or Layout Views. We can use the same partial view and eliminate the redundant code at multiple places.

Use of ASP.NET Razor Partial View

When having a large view file containing several logical sections, we can break it into smaller components that can render as a partial view. ASP.NET Razor Partial View works on the DRY (Don't Repeat Yourself) concept.

Creating an ASP.NET Razor Partial View

To create a partial view, right-click on the Shared folder -> click Add -> click View to open the Add View popup.

 

You can create ASP.NET Razor Partial Views in any View folder. However, we recommend using all your partial views in the Shared folder in multiple views.

Enter a partial view name in the Add New Item popup and select the checkbox "Create as a partial view." We don't need to use any model for this partial view, so keep the Template dropdown as Empty (without model). Click the Add button to create an empty partial view in the Shared folder.

Rendering ASP.NET Razor Partial View

Using these HTML(HyperText Markup Language) helper methods, you can render the partial view in the parent view.

@Html.Partial()

The @Html.Partial() renders the specified partial view. It takes a partial view name as a string parameter and returns MvcHtmlString. It gives an HTML string, so you have a chance of altering the HTML before rendering.

@Html.RenderPartial()

The @html.RenderPartial() is the same as the @html.Partial() process except that it writes the resulting HTML of a specified partial view into an HTTP(Hypertext Transfer Protocol) response stream directly. So, you can modify its HTML before rendering.

@Html.Action()

This @html.Action() generates a partial view as an HTML string to store it in another string variable. It is a string return type method, so first, it returns the result as a string then renders the result to the response.

@Html.RenderAction()

The @html.RenderAction() executes the specified action method and renders the outcome. The specified action method must be labeled with the [ChildActionOnly] attribute and return the PartialViewResult using the PartialView() method.

Working Example

Here is an example code demonstrating a partial view in a registration form.

// Form.cshtml

@{  
    ViewBag.Title = "Registration Form";  
}

<h3>Registration Form</h3>
<hr />
<div class="form-horizontal">
    @using (Html.BeginForm("Registration", "Students"))
    {
        <div class="form-group">
            @Html.Label("User Name:", new { @class = "control-label col-sm-3" })
            <div class="col-sm-8">
                @Html.TextBox("username", null, new { @class = "form-control" })
            </div>
        </div>
        @Html.Partial("PartialViewExample")   
        <div class="form-group">
            <div class="col-sm-3"></div>
            <div class="col-sm-8">
                <input type="submit" value="submit" class="btn-primary btn" />  
            </div>
        </div>
    }
</div>

 

//PartialViewExample.cshtml

<div class="form-group">  
    @Html.Label("Email ID", new { @class = "control-label col-sm-3" })  
    <div class="col-sm-8">  
        @Html.TextBox("email", null, new { @class = "form-control" })  
    </div>  
</div>

 

Output:-

Ideal:

Entering Details:

On submission:

FAQs

  1. What is ASP.NET?
    ASP.NET is an open-source, server-side web development framework designed to produce dynamic web pages. Microsoft developed it to allow programmers to build active websites, applications, and services.
     
  2. What is ASP.NET in C#?
    ASP.NET is a web-based application framework developed and marketed by Microsoft to let programmers build dynamic websites. It will enable you to use a full-featured programming language such as C# or VB.NET to build web applications quickly.
     
  3. What is the difference between ASP.NET and C#?
    The difference between ASP.NET and C# is that ASP.NET is an open-source framework for web application development to create dynamic content over web pages. C# is an object-oriented, functional, generic, imperative, and component-based programming language.
     
  4. What is a partial view in MVC?
    A partial view is a Razor markup file (. cshtml) without an @page directive that renders HTML output within another markup file's rendered output. We use the term partial view when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
     
  5. Why is MVC so popular?
    UI changes are still effortless, perhaps even more straightforward. In MVC, the Controller and View tend to mesh together. Layers create a strict separation. Both Layers are black boxes, free to vary independently in implementation.

Key Takeaways

This article teaches about ASP.NET Razor Partial Views and how we use them. We saw why ASP.NET Razor Partial Views could be beneficial for a developer to learn. 

Click here to read about the Top 10 web development frameworks. Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form. Check out our Web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Happy Learning!

Live masterclass